gpt4 book ai didi

php - Doctrine 获取加入

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:57 24 4
gpt4 key购买 nike

首先,我将给出一个带有一些伪代码的示例,然后我将解释问题所在。假设我有两个实体用户和电话号码。他们的关系是一对多的。在我的 UserRepository 中,我可以有类似的东西:

class UserRepository
{
public function getUser($id, $type)
{
$users = $this->createQuery("SELECT u, p FROM User u JOIN u.phonenumbers p
WHERE u.id = :id AND p.type = :type")
->setParameters(array(
'id' => $id,
'type' => $type,
))
->getResult();
return $users[0];
}
}

在我的应用程序中,如果我有类似的东西:

$user = $userRepo->getUser(1, 'home');
var_dump($user->getPhonenumbers()); // here phonenumbers collection is ok

$user = $userRepo->getUser(1, 'work');
var_dump($user->getPhonenumbers()); // Here phonenumbers collection is wrong.
// It's exactly the same as the previous one.

所以我的问题是:是否可以使用 fetch join(具有不同的条件)并每次都获得正确的集合?

最佳答案

Fetch 加入和过滤一个集合并不能很好地协同工作。以下是您应该如何操作:

SELECT 
u, p
FROM
User u
JOIN
u.phonenumbers p
JOIN
u.phonenumbers p2
WHERE
u.id = :id
AND
p2.type = :type

这对第二个连接的(未水合的)p2 应用过滤,从而导致正确的水合和过滤。

关于php - Doctrine 获取加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13603054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com