Pheasant\Relationship::addJoin PHP Method

addJoin() public static method

Adds a join clause to the given query for the given schema and relationship. Optionally takes a nested list of relationships that will be recursively joined as needed.
public static addJoin ( $query, $parentAlias, $schema, $relName, $nested = [], $joinType = 'inner' ) : void
return void
    public static function addJoin($query, $parentAlias, $schema, $relName, $nested = array(), $joinType = 'inner')
    {
        if (!in_array($joinType, array('inner', 'left', 'right'))) {
            throw new \InvalidArgumentException("Unsupported join type: {$joinType}");
        }
        list($relName, $alias) = self::parseRelName($relName);
        $rel = $schema->relationship($relName);
        // look up schema and table for both sides of join
        $instance = \Pheasant::instance();
        $localTable = $instance->mapperFor($schema->className())->table();
        $remoteSchema = $instance->schema($rel->class);
        $remoteTable = $instance->mapperFor($rel->class)->table();
        $joinMethod = $joinType . 'Join';
        $query->{$joinMethod}($remoteTable->name()->table, sprintf('ON `%s`.`%s`=`%s`.`%s`', $parentAlias, $rel->local, $alias, $rel->foreign), $alias);
        foreach (self::normalizeMap($nested) as $relName => $nested) {
            self::addJoin($query, $alias, $remoteSchema, $relName, $nested, $joinType);
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Join other related objects into a collection for the purpose of filtering. Relationships
  * is either a flat array of relationships (as defined in the object's schema) or a nested array
  * @chainable
  */
 public function join($rels, $joinType = 'inner')
 {
     $schemaAlias = $this->_schema->alias();
     foreach (Relationship::normalizeMap($rels) as $alias => $nested) {
         Relationship::addJoin($this->_queryForWrite(), $schemaAlias, $this->_schema, $alias, $nested, $joinType);
     }
     return $this;
 }