Nette\Database\Helpers::findDuplicates PHP Method

findDuplicates() public static method

Finds duplicate columns in select statement
public static findDuplicates ( PDOStatement $statement ) : string
$statement PDOStatement
return string
    public static function findDuplicates(\PDOStatement $statement)
    {
        $cols = [];
        for ($i = 0; $i < $statement->columnCount(); $i++) {
            $meta = $statement->getColumnMeta($i);
            $cols[$meta['name']][] = isset($meta['table']) ? $meta['table'] : '';
        }
        $duplicates = [];
        foreach ($cols as $name => $tables) {
            if (count($tables) > 1) {
                $tables = array_filter(array_unique($tables));
                $duplicates[] = "'{$name}'" . ($tables ? ' (from ' . implode(', ', $tables) . ')' : '');
            }
        }
        return implode(', ', $duplicates);
    }

Usage Example

Example #1
0
 /**
  * @inheritDoc
  */
 public function fetch()
 {
     $data = $this->pdoStatement ? $this->pdoStatement->fetch() : NULL;
     if (!$data) {
         $this->pdoStatement->closeCursor();
         return FALSE;
     } elseif ($this->result === NULL && count($data) !== $this->pdoStatement->columnCount()) {
         $duplicates = Helpers::findDuplicates($this->pdoStatement);
         trigger_error("Found duplicate columns in database result set: {$duplicates}.", E_USER_NOTICE);
     }
     $row = new Row();
     foreach ($this->normalizeRow($data) as $key => $value) {
         if ($key !== '') {
             $row->{$key} = $value;
         }
     }
     $this->resultKey++;
     return $this->result = $row;
 }