lithium\data\Model::relations PHP Method

relations() public static method

Returns a list of models related to Model, or a list of models related to this model, but of a certain type.
public static relations ( string $type = null ) : array | object | void
$type string A type of model relation.
return array | object | void An array of relation instances or an instance of relation.
    public static function relations($type = null)
    {
        $self = static::_object();
        if ($type === null) {
            return static::_relations();
        }
        if (isset($self->_relationFieldNames[$type])) {
            $type = $self->_relationFieldNames[$type];
        }
        if (isset($self->_relations[$type])) {
            return $self->_relations[$type];
        }
        if (isset($self->_relationsToLoad[$type])) {
            return static::_relations(null, $type);
        }
        if (in_array($type, $self->_relationTypes, true)) {
            return array_keys(static::_relations($type));
        }
    }

Usage Example

Example #1
0
 /**
  * Returns a list of models related to `Model`, or a list of models related
  * to this model, but of a certain type. Overwritten to return all/original/alternate relations
  *
  * @param string $name A type of model relation.
  * @param string $type Specify all, original, alternate
  * @return array An array of relation types.
  */
 public static function relations($name = null, $type = 'original')
 {
     if ($type == 'all' || $type == 'alternate') {
         $self = static::_object();
         if ($type == 'all') {
             $relations = array_merge($self->_relations, $self->_alternateRelations);
         } else {
             $relations = $self->_alternateRelations;
         }
         if (!$name) {
             return $relations;
         }
         if (isset($relations[$name])) {
             return $relations[$name];
         }
         if (isset($self->_relationTypes[$name])) {
             return array_keys(array_filter($relations, function ($i) use($name) {
                 return $i->data('type') == $name;
             }));
         }
     }
     return parent::relations($name);
 }