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

  • 写回答

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 !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?