Bluz\Db\Row::getTable PHP Method

getTable() public method

Returns the table object, or null if this is disconnected row
public getTable ( ) : Table
return Table
    public function getTable()
    {
        if ($this->table instanceof Table) {
            return $this->table;
        }
        if ($this->tableClass) {
            $tableClass = $this->tableClass;
        } else {
            // try to guess table class
            $rowClass = get_class($this);
            /**
             * @var string $tableClass is child of \Bluz\Db\Table
             */
            $tableClass = substr($rowClass, 0, strrpos($rowClass, '\\', 1) + 1) . 'Table';
        }
        // check class initialization
        if (!class_exists($tableClass) || !is_subclass_of($tableClass, '\\Bluz\\Db\\Table')) {
            throw new TableNotFoundException("`Table` class is not exists or not initialized");
        }
        /**
         * @var Table $tableClass
         */
        $table = $tableClass::getInstance();
        $this->setTable($table);
        return $table;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * findRelation
  *
  * @param  Row $row
  * @param  string $relation
  * @return array
  * @throws Exception\RelationNotFoundException
  */
 public static function findRelation($row, $relation)
 {
     $model = $row->getTable()->getModel();
     /** @var \Bluz\Db\Table $relationTable */
     $relationTable = Relations::getModelClass($relation);
     $relationTable::getInstance();
     if (!($relations = Relations::getRelations($model, $relation))) {
         throw new RelationNotFoundException("Relations between model `{$model}` and `{$relation}` is not defined");
     }
     // check many-to-many relations
     if (sizeof($relations) == 1) {
         $relations = Relations::getRelations($model, current($relations));
     }
     $field = $relations[$model];
     $key = $row->{$field};
     return Relations::findRelations($model, $relation, [$key]);
 }
All Usage Examples Of Bluz\Db\Row::getTable