Symfony 如何使用查询生成器通过使用负面条件筛选相关实体?
我的实体:Symfony 如何使用查询生成器通过使用负面条件筛选相关实体?,symfony,many-to-many,query-builder,entities,arraycollection,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->productg
/**
* @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 id6
没有关系的所有数组
直到现在,我才知道如何删除与productgroup 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();
}
我相信您使用expr的方式不正确,请尝试使用以下方式:
$qb->expr()->notIn('f.productgroup', [6])
您可以在此处阅读更多内容我测试了您的建议,发现“productgroup”附近第70列第0行出现错误
[语义错误]:错误:无效的PathExpression。应为StateFieldPathExpression或SingleValuedAssociationField。
是否尝试了->其中(“:id不是f.productgroup的成员”)
?其中id可以是您案例中的实体id或整个实体6。