学说获取连接

Doctrine fetch join(学说获取连接)
本文介绍了学说获取连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

Fetch 加入和过滤集合并不是很好地协同工作的事情.你应该这样做:

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

这会在第二个加入(而不是水合)p2 上应用过滤,从而产生正确的水合和过滤.

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

这篇关于学说获取连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)