学说撷取加入

编程入门 行业动态 更新时间:2024-10-12 18:19:35
本文介绍了学说撷取加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 首先我会举一些伪代码的例子,然后我将解释什么是问题。让我说我有两个实体User和Phonenumber。他们的关系是一对多的。在我的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]; } }

在我的应用程序中,如果我有这样的东西: p>

$ user = $ userRepo-> getUser(1,'home'); var_dump($ user-> getPhonenumbers()); //这里phonenumbers集合是ok $ user = $ userRepo> getUser(1,'work'); var_dump($ user-> getPhonenumbers()); //这里phonenumbers收藏是错误的。 //与前一个完全一样。

所以我的问题是:是否可以使用提取连接(具有不同的条件)每次正确收集?

解决方案

获取加入和过滤集合并不是很好地协同工作。以下是您应该如何做:

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

这对第二个加入(而不是水合)应用过滤器 p2 ,这会导致正确的水合和过滤。

First I will give an example with some pseudo code and then I will explain what is the problem. Let me say I have two entities User and Phonenumber. Their relation is one-to-many. In my UserRepository I can have something like that:

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]; } }

In my app if I have something like:

$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.

So my questions is: Is it possible to use fetch join (with different criteria) and to get the proper collection each time?

解决方案

Fetch joining and filtering a collection are not things that work together quite well. Here's how you should do it:

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

This applies filtering on the second joined (and not hydrated) p2, which results in correct hydration and filtering.

更多推荐

学说撷取加入

本文发布于:2023-10-11 14:27:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481968.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:学说

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!