使用JOIN和WHERE时的学说问题

时间:2009-12-30 05:36:34

标签: php orm doctrine

我有一个简单的学说代码:

$dql = Doctrine_Query::create()
        ->select('u.ident, u.username, u.email')
        ->from('Users u, u.Attributes ua');
if ($query) {
  $dql->where('u.username LIKE ?', "%$query%")
    ->orWhere('u.name LIKE ?', "%$query%")
    ->orWhere('u.email LIKE ?', "%$query%");
}
$dql->offset($start)->limit($count);
$users = $dql->execute();

使用分析器,我发现Doctrine正在将它作为两个单独的查询运行:

SELECT DISTINCT e3.ident FROM users e3 
LEFT JOIN userpolicies e4 ON e3.ident = e4.user_id 
WHERE e3.username LIKE ? OR e3.name LIKE ? OR e3.email LIKE ? LIMIT 20

Params:
Array
(
    [0] => %fam%
    [1] => %fam%
    [2] => %fam%
)

SELECT e.ident AS e__ident, e.username AS e__username, e.email AS e__email FROM users e
LEFT JOIN userpolicies e2 ON e.ident = e2.user_id 
WHERE e.ident IN ('2', '40', '42', '44', '52', '53', '54', '55', '56', '58', '60', '61', '62', '64', '65', '66', '68', '70', '74', '82') AND e.username LIKE ? OR e.name LIKE ? OR e.email LIKE ?

Params:
Array
(
    [0] => %fam%
    [1] => %fam%
    [2] => %fam%
)

这并不是我想到的,因为即使在第一个查询中选择了“Users”的匹配行之后第二个查询也会再次应用WHERE条件(即,第二个查询应该以{{1​​停止) }})。这是我的搞砸了还是Doctrine的预期行为?我正在使用教义v1.1.6。

1 个答案:

答案 0 :(得分:2)

好吧,看起来这个问题已在Doctrine 1.2版中修复。现在切换到它:)抱歉,伙计们。