Nette\Database\Table\Selection::getReferencedTable PHP Метод

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

Returns referenced row.
public getReferencedTable ( ActiveRow $row, $table, $column = NULL ) : ActiveRow | null | FALSE
$row ActiveRow
Результат ActiveRow | null | FALSE NULL if the row does not exist, FALSE if the relationship does not exist
    public function getReferencedTable(ActiveRow $row, $table, $column = NULL)
    {
        if (!$column) {
            $belongsTo = $this->conventions->getBelongsToReference($this->name, $table);
            if (!$belongsTo) {
                return FALSE;
            }
            list($table, $column) = $belongsTo;
        }
        if (!$row->accessColumn($column)) {
            return FALSE;
        }
        $checkPrimaryKey = $row[$column];
        $referenced =& $this->refCache['referenced'][$this->getSpecificCacheKey()]["{$table}.{$column}"];
        $selection =& $referenced['selection'];
        $cacheKeys =& $referenced['cacheKeys'];
        if ($selection === NULL || $checkPrimaryKey !== NULL && !isset($cacheKeys[$checkPrimaryKey])) {
            $this->execute();
            $cacheKeys = [];
            foreach ($this->rows as $row) {
                if ($row[$column] === NULL) {
                    continue;
                }
                $key = $row[$column];
                $cacheKeys[$key] = TRUE;
            }
            if ($cacheKeys) {
                $selection = $this->createSelectionInstance($table);
                $selection->where($selection->getPrimary(), array_keys($cacheKeys));
            } else {
                $selection = [];
            }
        }
        return isset($selection[$checkPrimaryKey]) ? $selection[$checkPrimaryKey] : NULL;
    }

Usage Example

Пример #1
0
 public function &__get($key)
 {
     if (array_key_exists($key, $this->data)) {
         $this->access($key);
         return $this->data[$key];
     }
     $column = $this->table->connection->databaseReflection->getReferencedColumn($key, $this->table->name);
     if (array_key_exists($column, $this->data)) {
         $value = $this->data[$column];
         $referenced = $this->table->getReferencedTable($key);
         $ret = isset($referenced[$value]) ? $referenced[$value] : NULL;
         // referenced row may not exist
         return $ret;
     }
     $this->access($key);
     if (array_key_exists($key, $this->data)) {
         return $this->data[$key];
     } else {
         $this->access($key, TRUE);
         $this->access($column);
         if (array_key_exists($column, $this->data)) {
             $value = $this->data[$column];
             $referenced = $this->table->getReferencedTable($key);
             $ret = isset($referenced[$value]) ? $referenced[$value] : NULL;
             // referenced row may not exist
         } else {
             $this->access($column, TRUE);
             trigger_error("Unknown column {$key}", E_USER_WARNING);
             $ret = NULL;
         }
         return $ret;
     }
 }
All Usage Examples Of Nette\Database\Table\Selection::getReferencedTable