Esempio n. 1
0
 /**
  * Generates the join statement for many-many relationship.
  * @param CDbTableSchema $joinTable the join table
  * @param array $fks the foreign keys
  * @param CJoinElement $parent the parent join element
  * @return string the join statement
  * @throws CDbException 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 = array();
     $childCondition = array();
     $fkDefined = true;
     foreach ($fks as $i => $fk) {
         if (!isset($joinTable->columns[$fk])) {
             throw new CDbException(Yii::t('yii', '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}".', array('{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);
             } else {
                 if (!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 = array();
         $childCondition = array();
         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 !== array() && $childCondition !== array()) {
         $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 CDbException(Yii::t('yii', '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.', array('{class}' => get_class($parent->model), '{relation}' => $this->relation->name)));
     }
 }