Skip to content
Permalink
Browse files

Inverted the order in which the joins for belongsToMany are added

  • Loading branch information...
lorenzo committed Apr 30, 2016
1 parent 4aab2bf commit c55d5530ea5447436c3c752972ef54d603dc521c
Showing with 10 additions and 6 deletions.
  1. +10 −6 src/ORM/Association/BelongsToMany.php
@@ -330,8 +330,6 @@ protected function _generateJunctionAssociations($junction, $source, $target)
*/
public function attachTo(Query $query, array $options = [])
{
parent::attachTo($query, $options);
$junction = $this->junction();
$belongsTo = $junction->association($this->source()->alias());
$cond = $belongsTo->_joinCondition(['foreignKey' => $belongsTo->foreignKey()]);
@@ -344,14 +342,20 @@ public function attachTo(Query $query, array $options = [])
// Attach the junction table as well we need it to populate _joinData.
$assoc = $this->_targetTable->association($junction->alias());
$query->removeJoin($assoc->name());
$options = array_intersect_key($options, ['joinType' => 1, 'fields' => 1]);
$options += [
$newOptions = array_intersect_key($options, ['joinType' => 1, 'fields' => 1]);
$newOptions += [
'conditions' => $cond,
'includeFields' => $includeFields,
'foreignKey' => $this->targetForeignKey(),
'foreignKey' => false,
];
$assoc->attachTo($query, $options);
$assoc->attachTo($query, $newOptions);
$query->eagerLoader()->addToJoinsMap($junction->alias(), $assoc, true);
parent::attachTo($query, $options);
$foreignKey = $this->targetForeignKey();
$thisJoin = $query->clause('join')[$this->name()];
$thisJoin['conditions']->add($assoc->_joinCondition(['foreignKey' => $foreignKey]));
}
/**

0 comments on commit c55d553

Please sign in to comment.
You can’t perform that action at this time.