Example #1
0
 /**
  * Применяет JOIN'ы для MANY_TO_MANY через bridge-коллекцию
  * @param ISelectBuilder $selectBuilder
  * @param ManyToManyRelationField $relationField поле по которому осуществляется связь
  * @param string $relationSourceAlias алиас таблицы для $relationField
  * @param ICollection $relationCollection коллекция поля ПО которому осуществляется связь
  * @param ICollection $targetCollection коллекция НА которую осуществляется связь
  * @return $this
  */
 protected function applyJoinForManyToManyRelation(ISelectBuilder $selectBuilder, ManyToManyRelationField $relationField, $relationSourceAlias, ICollection $relationCollection, ICollection $targetCollection)
 {
     $targetTableName = $targetCollection->getMetadata()->getCollectionDataSource()->getSourceName();
     $targetTableAlias = $relationSourceAlias . self::ALIAS_SEPARATOR . $relationField->getName();
     $targetPk = $targetCollection->getIdentifyField();
     $relationPk = $relationCollection->getIdentifyField();
     $metadata = $this->getMetadataManager()->getMetadata($relationField->getBridgeCollectionName());
     $bridgeTableName = $metadata->getCollectionDataSource()->getSourceName();
     $bridgeTableAlias = $targetTableAlias . self::BRIDGE_ALIAS_POSTFIX;
     $bridgeRelationFieldName = $relationField->getRelatedFieldName();
     $bridgeRelationField = $metadata->getField($bridgeRelationFieldName);
     $bridgeTargetFieldName = $relationField->getTargetFieldName();
     $bridgeTargetField = $metadata->getField($bridgeTargetFieldName);
     // JOIN bridge collection
     $leftColumn = $bridgeTableAlias . self::FIELD_SEPARATOR . $bridgeRelationField->getColumnName();
     $rightColumn = $relationSourceAlias . self::FIELD_SEPARATOR . $relationPk->getColumnName();
     $selectBuilder->leftJoin(array($bridgeTableName, $bridgeTableAlias))->on($leftColumn, '=', $rightColumn);
     // JOIN target collection
     $leftColumn = $targetTableAlias . self::FIELD_SEPARATOR . $targetPk->getColumnName();
     $rightColumn = $bridgeTableAlias . self::FIELD_SEPARATOR . $bridgeTargetField->getColumnName();
     $selectBuilder->leftJoin(array($targetTableName, $targetTableAlias))->on($leftColumn, '=', $rightColumn);
     return $this;
 }