数组不在数组中用于学说查询 [英] Array not in array for doctrine query

查看:110
本文介绍了数组不在数组中用于学说查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用数组不在数组之类的条件。我有一个数组,其中有两个要搜索的元素。例如



(3,1)NOT IN((2,3),(1,3),(1,32))



它作为phpmyadmin中的SQL查询工作。

  $ em = $ this-> getDoctrine()-> getManager(); 



$ qb = $ em-> getRepository('FangoUserBundle:User')
-> createQueryBuilder('user')
-> leftJoin('user.collabInvitationTarget ','invite')
-> where('(:: currentUserId,user.id)NOT IN(:inviteArr)')
-> setParameter('currentUserId',$ this-> getUser()-> getId())
-> setParameter('inviteArr',$ invites,\Doctrine\DBAL\Connection :: PARAM_INT_ARRAY);

在呈现模板期间引发了异常( [语法错误]行0,列304:错误:在FangoCollabaignBundle:Default:index.html.twig中,在第54行。

任何建议吗?

解决方案

这可能不是最终答案,但此处需要纠正一些事情。


  1. 虽然在基本SQL中是允许的,但查询应在双引号之间,而字符串应在单引号之间。并非相反。


  2. 从您的代码中,我猜测您的自定义查询在控制器中。最好将它与一个新函数一起移入实体存储库,然后在控制器中调用此函数。

    奇怪的是,对于,您不会收到任何错误createQueryBuilder(),因为它在此上下文中不存在。


  3. 避免别名可能会误认为实体名称。


  4. 您的查询在我认为的 where()周围是错误的。


考虑到您应用了第2点,这就是我会尝试这样做的方式。

 公共函数getNameYourFunction($ user,$ invites){
$ em = $ this-> getEntityManager();
$ qb = $ em-> createQueryBuilder()

$ qb-> select( u)
-> from(User :: class, u )
-> leftJoin( u.collabInvitationTarget, i)
->其中($ qb-> expr()-> notin( u.id, (:inviteArrA)))
-> andWhere($ qb-> expr()-> notin(:currentUserId,(:inviteArrB)))
-> setParameters (array(
'currentUserId'=> $ user-> getId(),
'inviteArrA'=> implode(',',$ invites),
'inviteArrB'= > implode(',',$ invites),
));
}

您会注意到我正在使用 User: :class

确保通过添加以下行将其导入存储库中:

使用FangoUserBundle\Entity\用户; (在您的情况下,使用可能有所不同)



会根据您的代码执行。
告诉我们是否可以解决您的问题。


I need to use a condition like array is not in array. I have array which one has two element for search. For example

(3,1) NOT IN ((2,3),(1,3),(1,32))

it's work as SQL query in phpmyadmin. But not worked as doctrine query.

$em = $this->getDoctrine()->getManager(); 

$qb = $em->getRepository('FangoUserBundle:User')
        ->createQueryBuilder('user')  
        ->leftJoin('user.collabInvitationTarget', 'invite')
        ->where('(:currentUserId, user.id) NOT IN (:inviteArr)')
        ->setParameter('currentUserId', $this->getUser()->getId())
        ->setParameter('inviteArr', $invites, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY);

An exception has been thrown during the rendering of a template ("[Syntax Error] line 0, col 304: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','") in FangoCollabaignBundle:Default:index.html.twig at line 54.

Any advice ?

解决方案

This might not be the final answer, but there are a few things to correct here.

  1. While it's tolerated in basic SQL, query should be between double quotes, and string should be between simple quotes. Not the opposite.

  2. From your code, I'm guessing that your custom query is in the controller. It would be best to move it into the entity repository, withing a new function, and then call this function in your controller.
    It's odd that you don't get an error for createQueryBuilder() as it doesn't exists in this context.

  3. Avoid alias that could be mistaken for entity names.

  4. Your query is wrong around the where() I think.

Taking in account that you applied point 2, this is how I would try to do it.

public function getNameYourFunction($user, $invites) {
    $em=$this->getEntityManager();
    $qb=$em->createQueryBuilder()

    $qb->select("u")
       ->from(User::class, "u")
       ->leftJoin("u.collabInvitationTarget", "i")
       ->where($qb->expr()->notin("u.id", "(:inviteArrA)"))
       ->andWhere($qb->expr()->notin(":currentUserId", "(:inviteArrB)"))
       ->setParameters(array(
           'currentUserId'=>$user->getId(),
           'inviteArrA'=>implode(',', $invites),
           'inviteArrB'=>implode(',', $invites),
       ));
}

You will notice that I'm using User::class.
Make sure to import it in your repository by adding this line :
use FangoUserBundle\Entity\User; (may be different usein your case)

This is what I would do based on your code. Tell us if it solved your problem.

这篇关于数组不在数组中用于学说查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆