Use of Propel's addMultipleJoin
Following on from a previous question about sub-selects, I have an SQL statement with multiple criteria on the join, like so:
SELECT * FROM person
LEFT O开发者_如何学JAVAUTER JOIN group_membership
ON person.id = group_membership.person_id
AND group_id = 1
WHERE group_membership.person_id is null;
Unfortunately, Propel, the ORM I am using with Symfony, doesn't seem very adept using multiple joins, and previous people have tried to hack criteria together to achieve results. Doing this confused Propel, and it started using CROSS JOINs. From what I've read, this was because Propel couldn't recognise the field ID of the second parameter:
$criteria->addJoin(self::ID, GroupMembershipPeer::PERSON_ID . ' AND ' .
GroupMembershipPeer::GROUP_ID . '=' . $group_id,
Criteria::LEFT_JOIN);
I then found the addMultipleJoin() method for Propel 1.4, which appears to work to some extent. I don't entirely understand it, or how I might call it to get what I desire:
$criteria->addMultipleJoin(array(
array(
'left' => self::ID,
'right' => GroupMembershipPeer::PERSON_ID,
'operator' => Criteria::EQUAL
),
array(
'left' => GroupMembershipPeer::GROUP_ID,
'right' => $group_id,
'operator' => Criteria::EQUAL
),
), Criteria::LEFT_JOIN);
This results in some strange SQL:
SELECT DISTINCT FROM `person` CROSS JOIN `group_membership` LEFT JOIN ON (= AND =) WHERE person.ID IN (3,5,17) AND group_membership.PERSON_ID IS NULL LIMIT 10
And PHP is throwing some strange errors:
Notice: Undefined offset: 0 in /path/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel/util/Criteria.php on line 675
Does anyone know whether it is myself or Propel doing something wrong, and how I might fix it, or accomplish what I need?
Maybe I am wrong, but why are you didn't use something like this:
$c->addJoin(array(self::ID, GroupMembershipPeer::GROUP_ID), array(GroupMembershipPeer::PERSON_ID, $group_id) );
Criteria supports multiple join conditions since Propel 1.3. Criteria doc
精彩评论