/** * */ private function destroyJoinTree() { if ($this->_joinTree !== null) { $this->_joinTree->destroy(); } $this->_joinTree = null; }
/** * Generates the join statement for many-many relationship. * @param \Database\schema\TableSchema $joinTable the join table * @param array $fks the foreign keys * @param \Database\ActiveRecord\Join\Element $parent the parent join element * @return string the join statement * @throws \Exception if a foreign key is invalid */ private function joinManyMany($joinTable, $fks, $parent) { $schema = $this->_builder->getSchema(); $joinAlias = $schema->quoteTableName($this->relation->name . '_' . $this->tableAlias); $parentCondition = []; $childCondition = []; $fkDefined = true; foreach ($fks as $fk) { if (!isset($joinTable->columns[$fk])) { throw new \Database\Exception('The relation "{relation}" in active record class "{class}" is specified with an invalid foreign key "{key}". There is no such column in the table "{table}".', ['{class}' => get_class($parent->model), '{relation}' => $this->relation->name, '{key}' => $fk, '{table}' => $joinTable->name]); } if (isset($joinTable->foreignKeys[$fk])) { list($tableName, $pk) = $joinTable->foreignKeys[$fk]; if (!isset($parentCondition[$pk]) && $schema->compareTableNames($parent->_table->rawName, $tableName)) { $parentCondition[$pk] = $parent->getColumnPrefix() . $schema->quoteColumnName($pk) . '=' . $joinAlias . '.' . $schema->quoteColumnName($fk); } elseif (!isset($childCondition[$pk]) && $schema->compareTableNames($this->_table->rawName, $tableName)) { $childCondition[$pk] = $this->getColumnPrefix() . $schema->quoteColumnName($pk) . '=' . $joinAlias . '.' . $schema->quoteColumnName($fk); } else { $fkDefined = false; break; } } else { $fkDefined = false; break; } } if (!$fkDefined) { $parentCondition = []; $childCondition = []; foreach ($fks as $i => $fk) { if ($i < count($parent->_table->primaryKey)) { $pk = is_array($parent->_table->primaryKey) ? $parent->_table->primaryKey[$i] : $parent->_table->primaryKey; $parentCondition[$pk] = $parent->getColumnPrefix() . $schema->quoteColumnName($pk) . '=' . $joinAlias . '.' . $schema->quoteColumnName($fk); } else { $j = $i - count($parent->_table->primaryKey); $pk = is_array($this->_table->primaryKey) ? $this->_table->primaryKey[$j] : $this->_table->primaryKey; $childCondition[$pk] = $this->getColumnPrefix() . $schema->quoteColumnName($pk) . '=' . $joinAlias . '.' . $schema->quoteColumnName($fk); } } } if ($parentCondition !== [] && $childCondition !== []) { $join = $this->relation->joinType . ' ' . $joinTable->rawName . ' ' . $joinAlias; $join .= ' ON (' . implode(') AND (', $parentCondition) . ')'; $join .= ' ' . $this->relation->joinType . ' ' . $this->getTableNameWithAlias(); $join .= ' ON (' . implode(') AND (', $childCondition) . ')'; if (!empty($this->relation->on)) { $join .= ' AND (' . $this->relation->on . ')'; } return $join; } else { throw new \Database\Exception('The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.', ['{class}' => get_class($parent->model), '{relation}' => $this->relation->name]); } }
/** * Joins with another join element * @param \Database\ActiveRecord\Join\Element $element the element to be joined */ public function join($element) { if ($element->slave !== null) { $this->join($element->slave); } if (!empty($element->relation->select)) { $this->selects[] = $element->getColumnSelect($element->relation->select); } $this->conditions[] = $element->relation->condition; $this->orders[] = $element->relation->order; $this->joins[] = $element->getJoinCondition(); $this->joins[] = $element->relation->join; $this->groups[] = $element->relation->group; $this->havings[] = $element->relation->having; if (is_array($element->relation->params)) { if (is_array($this->params)) { $this->params = array_merge($this->params, $element->relation->params); } else { $this->params = $element->relation->params; } } $this->elements[$element->id] = true; }