lithium\data\model\Query::relationships PHP Method

relationships() public method

Set and get the relationships.
public relationships ( string $relpath = null, array $config = null ) : mixed
$relpath string A dotted path.
$config array the config array to set.
return mixed The relationships array or a relationship array if `$relpath` is set. Returns `null` if a join doesn't exist.
    public function relationships($relpath = null, $config = null)
    {
        if ($config) {
            if (!$relpath) {
                throw new InvalidArgumentException("The relation dotted path is empty.");
            }
            if (isset($config['model']) && isset($config['alias'])) {
                $this->_models[$config['alias']] = $config['model'];
            }
            $this->_config['relationships'][$relpath] = $config;
            return $this;
        }
        if (!$relpath) {
            return $this->_config['relationships'];
        }
        if (isset($this->_config['relationships'][$relpath])) {
            return $this->_config['relationships'][$relpath];
        }
    }

Usage Example

Example #1
0
 public function testRelationships()
 {
     $query = new Query(array('relationships' => array('Model1' => array('foo' => 'bar'))));
     $query->relationships('Model1.Model2', array('bar' => 'baz'));
     $expected = array('Model1' => array('foo' => 'bar'), 'Model1.Model2' => array('bar' => 'baz'));
     $relationships = $query->relationships();
     $this->assertEqual($expected, $relationships);
     $query = new Query();
     $query->relationships('Model1', array('foo' => 'bar'));
     $query->relationships('Model1.Model2', array('bar' => 'baz'));
     $relationships = $query->relationships();
     $this->assertEqual($expected, $relationships);
 }