douyi5822 2015-06-23 09:29
浏览 53
已采纳

Symfony将DQL表达式与“NOT IN”连接在一起

I'm using Symfony2 and i'm trying to get an array of languages not associated to a specified client.

I have a Client entity indicating the Client, ClientLanguage that has the following structure:

  • id_menu_language PRIMARY KEY
  • language the association with the Language entity
  • client the association with the Client entity
  • sequence tells the order the language should be shown (not used here)

and Language Entity.

To get an array of languages not associated to the client i want to proceed in the following way:

  • Get the languages that the client has already associated (and I'm getting the correct DQL in the $clientLanguagesDQL variable)
  • Retrieve a list of all the available languages
  • Exclude from that list all the languages already associated to the client (by using NOT IN (...) ).

This is the function I wrote to accomlish that:

<?php

namespace AppBundle\Repository;

use AppBundle\Entity\Client;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;

class ClientRepository extends EntityRepository
{

    /**
     * @param $client Client to check
     * @return array
     */
    public function getLanguagesNotAssociatedToClient($client)
    {
        $qb = $this->getEntityManager()->createQueryBuilder();

        $clientLanguagesDQL = $qb
            ->select('lang')
            ->from('AppBundle:Language', 'lang')
            ->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
            ->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
            ->where('client.idClient = :client_id')
            ->getQuery()
            ->getDQL();

        $languages = $qb->select('language')
            ->from('AppBundle:Language', 'language')
            ->where($qb->expr()->notIn('language', $clientLanguagesDQL))
            ->setParameter('client_id', $client->getIdClient())
            ->getQuery()
            ->getResult();

        return $languages;

    }
}

However, when I run this, Symfony complains about a : [Semantical Error] line 0, col 293 near 'lang INNER JOIN': Error: 'lang' is already defined. It also tells me that there is a QueryException, and it shows me the following query:

SELECT language 
FROM AppBundle:Language lang 
    INNER JOIN AppBundle:ClientLanguage languages_assoc 
        WITH languages_assoc.language = lang 
    INNER JOIN AppBundle:Client client 
        WITH languages_assoc.client = client, AppBundle:Language language WHERE language NOT IN(
    SELECT lang 
    FROM AppBundle:Language lang 
        INNER JOIN AppBundle:ClientLanguage languages_assoc 
            WITH languages_assoc.language = lang 
        INNER JOIN AppBundle:Client client 
            WITH languages_assoc.client = client 
    WHERE client.idClient = :client_id
)  

And this is definitely not what I want to do. Why there appeared to be two joins with AppBundle:ClientLanguage and AppBundle:Client? I use this association only in my first subquery.

If it can help, when I run this:

    $clientLanguagesDQL = $qb
        ->select('lang')
        ->from('AppBundle:Language', 'lang')
        ->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
        ->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
        ->where('client.idClient = :client_id')
        ->getQuery()
        ->getDQL();

This is the returned DQL stored in $clientLanguageDQL:

SELECT lang 
FROM AppBundle:Language lang 
    INNER JOIN AppBundle:ClientLanguage languages_assoc 
        WITH languages_assoc.language = lang 
    INNER JOIN AppBundle:Client client 
        WITH languages_assoc.client = client 
WHERE client.idClient = :client_id

What's wrong with this query?

  • 写回答

1条回答 默认 最新

  • douji6667 2015-06-24 07:02
    关注

    My mistake was very stupid, all I had to do is creating a new query builder for each query instead of reusing the first one:

        $qb1 = $this->getEntityManager()->createQueryBuilder();
    
        $clientLanguagesDQL = $qb1
            ->select('lang')
            ->from('AppBundle:Language', 'lang')
            ->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
            ->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
            ->where('client.idClient = :client_id')
            ->getQuery()
            ->getDQL();
    
        $qb2 = $this->getEntityManager()->createQueryBuilder();
        $languages = $qb2->select('language')
            ->from('AppBundle:Language', 'language')
            ->where($qb2->expr()->notIn('language', $clientLanguagesDQL))
            ->setParameter('client_id', $client->getIdClient())
            ->getQuery()
            ->getResult();
    
        return $languages;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等