Array not in array for doctrine query

喜夏-厌秋 提交于 2019-12-13 04:08:04

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/50311175/array-not-in-array-for-doctrine-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!