使用Propel的addMultipleJoin

时间:2011-07-29 19:58:56

标签: php sql symfony1 symfony-1.4 propel

previous question关于子选择开始,我有一个在连接上有多个条件的SQL语句,如下所示:

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;

不幸的是,Propel,我在Symfony上使用的ORM,似乎不太熟练使用多个连接,而之前的人已经尝试hack criteria together来实现结果。做这个混淆Propel,它开始使用CROSS JOIN。从我read开始,这是因为Propel无法识别第二个参数的字段ID:

$criteria->addJoin(self::ID, GroupMembershipPeer::PERSON_ID . ' AND ' .
    GroupMembershipPeer::GROUP_ID . '=' . $group_id,
    Criteria::LEFT_JOIN);

然后我找到了Propel 1.4的addMultipleJoin()方法,它似乎在某种程度上起作用。我不完全理解它,或者我怎么称呼它来得到我想要的东西:

$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);

这导致一些奇怪的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

PHP正在抛出一些奇怪的错误:

Notice: Undefined offset: 0 in /path/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel/util/Criteria.php on line 675

有谁知道是我自己还是Propel做错了什么,以及我如何解决它,或完成我需要的工作?

1 个答案:

答案 0 :(得分:1)

也许我错了,但你为什么不使用这样的东西:

$c->addJoin(array(self::ID, GroupMembershipPeer::GROUP_ID), array(GroupMembershipPeer::PERSON_ID, $group_id) );

自Propel 1.3起,Criteria支持多种连接条件。 Criteria doc