简体   繁体   English

教义查询生成器:返回两个相同结果的数组的结果

[英]Doctrine Query Builder : Result returning array of two times the same result

I'm currently working on a Symfony project, using Doctrine to manage entities. 我目前正在研究一个Symfony项目,使用Doctrine来管理实体。

I have a table named User, containing a few columns, and then another table named Tag, containing a foreign key to that User table with a ManyToOne relation based on the user id, and a single other column named value. 我有一个名为User的表,其中包含几列,然后是另一个名为Tag的表,其中包含该User表的外键,该表具有基于用户ID的ManyToOne关系,以及另一个名为value的列。

In my app, I need to find a list of users, depending on one of the Tag row, AND the value of one of the User's column. 在我的应用中,我需要根据“标记”行之一和“用户”列之一的值查找用户列表。 Let's resume : 让我们继续:

Select all users where user.value equals somevalue AND Tag.value equals anothervalue. 选择user.value等于somevalue且Tag.value等于anothervalue的所有用户。

As I never used Symfony nor Doctrine before this project, I searched into Doctrine documentation and found about the Query Builder. 由于在此项目之前从未使用过Symfony或Doctrine,所以我搜索了Doctrine文档并找到了有关查询生成器的信息。 So, I did this : 所以,我这样做:

EDIT : The way I was doing it was kinda weird, so I modified it and here is the result : 编辑:我做的方式有点奇怪,所以我修改了它,这是结果:

public function findByTagAndApp($tag, $app)
{
    $em = $this->getEntityManager();
    $qb = $em
        ->getRepository('APIBundle:User')
        ->createQueryBuilder('u')
        ->leftJoin('APIBundle\Entity\Tag', 't')
        ->where('u.application = :app')
        ->andWhere('t.tag = :tag')
        ->setParameter('tag', $tag)
        ->setParameter('app', $app)
    ;
    $users = $qb->getQuery()->getResult();
    return $users;
}

And it seems like it works, but in a strange way. 看起来好像可行,但是以一种奇怪的方式。 Instead of returning an array of User items, which is what I want, it returns an array of array of User items. 而不是返回我想要的用户项数组,而是返回了一个用户项数组。 The first array is always containing two entries, and these two entries are always identical : they are the array I need, without a single difference. 第一个数组始终包含两个条目,而这两个条目始终相同:它们是我需要的数组,没有任何区别。

I tried to do return $users[0] instead of just users, and then I can manipulate my User entities the intended way. 我尝试返回$ users [0]而不是仅仅返回用户,然后可以按预期方式操作User实体。 I could keep it this way as it is working, but I'd really like to know why it returns an unneeded array of array instead of just the array I want. 我可以一直使用这种方式,但是我真的很想知道为什么它返回不需要的数组而不是我想要的数组。 It might be my query, but I'm not sure how to modify it to get only the Users I want. 这可能是我的查询,但是我不确定如何修改它以仅获取所需的用户。

Any clues on why it behave like this would be really appreciated, as I'm still learning about Doctrine. 由于我仍在学习Doctrine,因此对它为什么如此表现的任何线索都将非常感激。 Thanks ! 谢谢 !

EDIT² : Nevermind, this query seems completely incorrect too, as I got all users according to the $app value, but it seems like it never check if there is a row in the Tag table with a value of somevalue associated to a foreign key of the User table.. EDIT²:没关系,该查询似乎也完全不正确,因为我根据$ app值获取了所有用户,但似乎它从未检查过Tag表中是否存在与外键关联的somevalue值的行。用户表..

我不知道到底是为什么,但是..我想你不得不提到from(),例如->from('User', 'u')在这里可以找到更多

After a few hours of tweaking, I figured it out using SQL statement on PhpMyAdmin, so I could notice that there was a LOT of things that I was doing wrong : 经过几个小时的调整,我在PhpMyAdmin上使用SQL语句找出了问题,所以我注意到有很多事情我做错了:

First, the join was not correct. 首先,联接不正确。 My goal was to collect users that had a certain value in their own table, AND a value from the Tag table. 我的目标是收集在自己的表中具有特定值的用户,以及在Tag表中具有值的用户。 Using a left join, I was collecting users with their own value OR a the value from the Tag table. 使用左联接,我正在使用自己的值或Tag表中的值来收集用户。

Second : The $app value I was passing was an object of type Application (the Application field in my User table is a foreign key), and the query builder didn't know what to do with it. 第二:我传递的$ app值是Application类型的对象(User表中的Application字段是外键),查询构建器不知道该怎么做。 Passing the app_id instead of the app object solved the problem. 传递app_id而不是app对象解决了该问题。

Third : The way I collected result was wrong. 第三:我收集结果的方式是错误的。 Obviously, this query returns an array of User objects. 显然,此查询返回一个User对象数组。 And as I execute this query multiple times in a row, I had an array on which I used array_push to fill it with the data, thinking that pushing array1 with array2 would put array2 values into array1, but it was just putting array2 into array1, resulting to that array of arrays that was the initial problem. 当我连续执行此查询多次时,我有一个数组,在该数组上我使用array_push填充了数据,以为用array2推送array1会将array2的值放入array1,但这只是将array2放入array1,结果就是最初的问题。 Using array_merge instead of array_push, I am now able to collect all the results from the queries into a single array. 现在,使用array_merge而不是array_push,我可以将查询的所有结果收集到单个数组中。 A little array_unique on that to avoid redundancy, and everything is working as expected. 在其上添加一些array_unique可以避免冗余,并且一切正常。

Thanks to everyone who replied ! 感谢所有答复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM