简体   繁体   English

通过使用NOT IN将SQL转换为Doctrine 2查询生成器或DQL?

[英]Convert SQL to Doctrine 2 Query Builder or DQL by using NOT IN?

How would I convert the SQL bellow to Doctrine 2 Query Builder or DQL? 如何将SQL波纹管转换为Doctrine 2查询生成器或DQL?

SELECT tags.* 
FROM tags 
WHERE tags.id NOT IN (
                     SELECT tag_id AS totalTags 
                     FROM human_resource_tags 
                     WHERE human_resource_id=1)

Tag entity is as follows: 标签实体如下:

Tag entity 标签实体

HumanResource entity is as follows: HumanResource实体如下:

HumanResource entity 人力资源实体

Basically what I want to do is to select all Tag entities for one HumanResource entity that that HumanResource entity does not have already. 基本上,我要做的是为一个HumanResource实体没有的所有Human实体选择所有Tag实体。

I am really struggling here so any help is appreciated. 我在这里真的很挣扎,因此我们将不胜感激。

I am using Doctrine version 2.4.2. 我正在使用Doctrine版本2.4.2。

========================================================================== ================================================== ========================

All hail to FuzzyTree for pointers :) 所有人都向FuzzyTree致指点:)

I have slightly modified it and it works like a charm :) So this will get you all Tag entities for particular HumanResource entity that are not added to HumanResource entity yet :) 我对其进行了稍微的修改,它的工作方式就像一个魅力:)因此,这将为您提供尚未添加到HumanResource实体的特定HumanResource实体的所有Tag实体:)

SO THIS IS SOLUTION: 所以这是解决方案:

$q = $this->createQueryBuilder('t')
      ->where('t.name LIKE :name')
      ->andWhere('NOT EXISTS (
                    SELECT h
                    FROM HRAPIBundle:HumanResource h 
                    WHERE h.id = ' . $humanResource->getId() .
                    'AND h MEMBER of t.human_resources
                )')
      ->setParameter('name', "%".$query."%")
      ->getQuery();

You can achieve this using NOT EXISTS and MEMBER OF 您可以使用NOT EXISTSMEMBER OF来实现

$qb->select("t")
    ->from('HardCoreMore\HRAPIBundle\Entity\Tag', 't')
    ->where('NOT EXISTS (
        SELECT 1 
        FROM HardCoreMore\HRAPIBundle\Entity\HumanResource h 
        WHERE h.id = 1 
        AND h MEMBER of t.human_resources
      )');

Select your $hmEntity that you don't want then use the follow code: 选择不需要的$hmEntity ,然后使用以下代码:

 $em = $this->getDoctrine()->getManager();
 $result= $em->getRepository('HRAPIBundle:Tag')->findByHumanResource(!$hmEntity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM