Piwik\DataAccess\LogQueryBuilder\JoinTables::sort PHP Метод

sort() публичный Метод

public sort ( $cmpFunction )
    public function sort($cmpFunction)
    {
        // we do not use $this->uasort as we do not want to maintain keys
        $tables = $this->getTables();
        // we need to make sure first table always comes first, only sort tables after the first table
        $firstTable = array_shift($tables);
        usort($tables, function ($ta, $tb) use($tables, $cmpFunction) {
            $return = call_user_func($cmpFunction, $ta, $tb);
            if ($return === 0) {
                $indexA = array_search($ta, $tables);
                $indexB = array_search($tb, $tables);
                return $indexA - $indexB;
            }
            return $return;
        });
        array_unshift($tables, $firstTable);
        $this->exchangeArray($tables);
    }

Usage Example

Пример #1
0
 /**
  * Generate the join sql based on the needed tables
  * @throws Exception if tables can't be joined
  * @return array
  */
 public function generate()
 {
     /** @var LogTable[] $availableLogTables */
     $availableLogTables = array();
     $this->tables->sort(array($this, 'sortTablesForJoin'));
     foreach ($this->tables as $i => $table) {
         if (is_array($table)) {
             // join condition provided
             $alias = isset($table['tableAlias']) ? $table['tableAlias'] : $table['table'];
             $this->joinString .= " LEFT JOIN " . Common::prefixTable($table['table']) . " AS " . $alias . " ON " . $table['joinOn'];
             continue;
         }
         $tableSql = Common::prefixTable($table) . " AS {$table}";
         $logTable = $this->tables->getLogTable($table);
         if ($i == 0) {
             // first table
             $this->joinString .= $tableSql;
         } else {
             $join = $this->findJoinCriteriasForTables($logTable, $availableLogTables);
             if ($join === null) {
                 $availableLogTables[$table] = $logTable;
                 continue;
             }
             // the join sql the default way
             $this->joinString .= " LEFT JOIN {$tableSql} ON " . $join;
         }
         $availableLogTables[$table] = $logTable;
     }
 }