问题描述
继上一个问题之后子选择,我有一个带有多个连接条件的 SQL 语句,如下所示:
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;
不幸的是,Propel,我在 Symfony 中使用的 ORM,似乎不太擅长使用多个连接,以前的人曾尝试 结合hack标准以达到结果.这样做混淆了 Propel,它开始使用 CROSS JOIN.从我的 read,这是因为Propel无法识别第二个参数的字段ID:
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);
然后我找到了 addMultipleJoin()Propel 1.4 的方法,似乎在某种程度上有效.我不完全理解它,或者我如何调用它来获得我想要的东西:
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);
这会导致一些奇怪的 SQL:
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
PHP 抛出了一些奇怪的错误:
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
有谁知道是我自己还是 Propel 做错了,我该如何解决它,或者完成我需要的事情?
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 从 Propel 1.3 开始支持多种连接条件.标准文档
Criteria supports multiple join conditions since Propel 1.3. Criteria doc
这篇关于Propel 的 addMultipleJoin 的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!