2016-07-04 14:51
浏览 56

Doctrine Query Builder:结果返回两次相同结果的数组

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongzhouhao4316 2016-07-05 09:06

    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 !

    解决 无用
    打赏 举报

相关推荐 更多相似问题