简体   繁体   中英

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.

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.

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.

As I never used Symfony nor Doctrine before this project, I searched into Doctrine documentation and found about the Query Builder. 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. 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. 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..

我不知道到底是为什么,但是..我想你不得不提到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 :

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. Using a left join, I was collecting users with their own value OR a the value from the Tag table.

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. Passing the app_id instead of the app object solved the problem.

Third : The way I collected result was wrong. Obviously, this query returns an array of User objects. 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. Using array_merge instead of array_push, I am now able to collect all the results from the queries into a single array. A little array_unique on that to avoid redundancy, and everything is working as expected.

Thanks to everyone who replied !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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