Use of Propel's addMultipleJoin

寵の児 提交于 2019-12-23 04:41:04

问题


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 OUTER 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?


回答1:


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



来源:https://stackoverflow.com/questions/6878380/use-of-propels-addmultiplejoin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!