Bluz\Db\Relations::findRelation PHP Метод

findRelation() публичный статический Метод

findRelation
public static findRelation ( Row $row, string $relation ) : array
$row Row
$relation string
Результат array
    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]);
    }

Usage Example

Пример #1
0
 /**
  * Get relation by name
  * @param string $modelName
  * @throws RelationNotFoundException
  * @return array
  */
 public function getRelations($modelName)
 {
     if (!isset($this->relations[$modelName])) {
         $relation = Relations::findRelation($this, $modelName);
         if (empty($relation)) {
             throw new RelationNotFoundException('Can\'t found relation data for model "' . $modelName . '"');
         } else {
             $this->relations[$modelName] = $relation;
         }
     }
     return $this->relations[$modelName];
 }
All Usage Examples Of Bluz\Db\Relations::findRelation