Skip to content

Commit 708fb08

Browse files
committed
Make sure the inner join with the junction table happens first
1 parent 571fc63 commit 708fb08

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/ORM/Association/BelongsToMany.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,21 @@ protected function _collectJointEntities($sourceEntity, $targetEntities) {
873873
*/
874874
protected function _buildQuery($options) {
875875
$name = $this->_junctionAssociationName();
876-
return $this->_buildBaseQuery($options)->matching($name);
876+
$query = $this->_buildBaseQuery($options);
877+
$joins = $query->join() ?: [];
878+
$keys = $this->_linkField($options);
879+
880+
$matching = [
881+
$name => [
882+
'table' => $this->junction()->table(),
883+
'conditions' => $keys,
884+
'type' => 'INNER'
885+
]
886+
];
887+
888+
$joins = $matching + $joins;
889+
$query->join($joins, [], true)->matching($name);
890+
return $query;
877891
}
878892

879893
/**

0 commit comments

Comments
 (0)