如何使用否定条件过滤与查询生成器相关的实体?

时间:2019-02-12 16:25:25

标签: symfony many-to-many query-builder entities arraycollection

我的实体:

  /**
  * @ORM\ManyToMany(targetEntity="Productgroup", inversedBy="fields")
  * @ORM\JoinColumn(name="productgroup", referencedColumnName="id")
  */
  private $productgroup;

  public function getProductgroup()
  {
    return $this->productgroup;
  }

  public function setProductgroup($productgroup): self
  {
    $this->productgroup = $productgroup;

    return $this;
  }

  public function __construct()
  {
    $this->productgroup = new ArrayCollection();
  }

这是输出:

array:2 [▼
  0 => Fields {#7534 ▼
    -id: 3
    -name: "cat"
    -unique_id: "5a38c820ed"
    -productgroup: PersistentCollection {#7538 ▼
      -snapshot: array:1 [ …1]
      -owner: Fields {#7534}
      -association: array:20 [ …20]
      -em: EntityManager {#2889 …11}
      -backRefFieldName: "fields"
      -typeClass: ClassMetadata {#6568 …}
      -isDirty: false
      #collection: ArrayCollection {#7539 ▼
        -elements: array:1 [▼
          0 => Productgroup {#7220 ▼
            -id: 6
            -name: "Animals"
            -unique_id: "9e4ef1c46f"
            -fields: PersistentCollection {#7431 ▶}
          }
        ]
      }
      #initialized: true
    }
    -type: Type {#7615 ▶}
  }
  1 => Fields {#7616 ▼
    -id: 5
    -name: "horse"
    -unique_id: "c3890b9287"
    -productgroup: PersistentCollection {#7617 ▼
      -snapshot: []
      -owner: Fields {#7616}
      -association: array:20 [ …20]
      -em: EntityManager {#2889 …11}
      -backRefFieldName: "fields"
      -typeClass: ClassMetadata {#6568 …}
      -isDirty: false
      #collection: ArrayCollection {#7618 ▼
        -elements: []
      }
      #initialized: false
    }
    -type: Type {#7619 ▶}
  }
]

我想做的是删除所有与producgroup id 6不相关的数组。

直到现在,我可以弄清楚如何删除与产品组ID 6没有任何关系的所有阵列:

控制器:

$group = $this->getDoctrine()->getRepository($EntityName)->filterByColletion(6);

和存储库:

  public function filterByColletion($id)
    {
      return $this->createQueryBuilder('f')
      ->leftJoin('f.productgroup', 'pg')
      ->where('pg.id = :id')
      ->setParameter(':id', 6)
      ->getQuery()
      ->execute();
    }

结果是:

array:1 [▼
  0 => Fields {#7534 ▼
    -id: 3
    -name: "cat"
    -unique_id: "5a38c820ed"
    -productgroup: PersistentCollection {#7538 ▼
      -snapshot: array:1 [ …1]
      -owner: Fields {#7534}
      -association: array:20 [ …20]
      -em: EntityManager {#2889 …11}
      -backRefFieldName: "fields"
      -typeClass: ClassMetadata {#6568 …}
      -isDirty: false
      #collection: ArrayCollection {#7539 ▼
        -elements: array:1 [▼
          0 => Productgroup {#7220 ▼
            -id: 6
            -name: "Animals"
            -unique_id: "9e4ef1c46f"
            -fields: PersistentCollection {#7431 ▶}
          }
        ]
      }
      #initialized: true
    }
    -type: Type {#7615 ▼
      +__isInitialized__: true
      -id: 3
      -name: "password"
      -unique_id: "2ef6e55a1d"
      -label: "password"
       …2
    }
  }
]

但是我恰恰相反需要它。所以我尝试了:

   public function filterByColletion($id)
    {
      return $this->createQueryBuilder('f')
      ->leftJoin('f.productgroup', 'pg')
      ->where('pg.id != :id')
      ->setParameter(':id', 6)
      ->getQuery()
      ->execute();
    }

但这给了我一个空数组作为输出,而不是期望的horse字段。

我也尝试了另一种方法,但这不能解决以太坊:

public function filterByColletion($id)
{
  $em = $this->getEntityManager();
  $qb  = $this->_em->createQueryBuilder();

  return $this->createQueryBuilder('f')
  ->leftJoin('f.productgroup', 'pg')
  ->where($qb->expr()->notIn('pg.id',6))
  ->getQuery()
  ->execute();
}

1 个答案:

答案 0 :(得分:1)

我相信您使用的expr不正确,请尝试使用这种方式:

$qb->expr()->notIn('f.productgroup', [6])

您可以在此处How to use WHERE IN with Doctrine 2

了解更多信息