我有需要转换为学说查询生成器的查询,但是我遇到错误,如何正确执行?
我的查询
SELECT id FROM category
WHERE
EXISTS(SELECT 1 FROM category_relations WHERE main_category_id = category.id)
AND
NOT EXISTS(SELECT 1 FROM category_relations WHERE sub_category_id = category.id)
我猜想
$subYes = $this->getEntityManager()->createQueryBuilder();
$subYes->select("1");
$subYes->from(CategoryRelations::class,"cr_y");
$subYes->andWhere('cr_y.mainCategory = c');
$subNot = $this->getEntityManager()->createQueryBuilder();
$subNot->select("1");
$subNot->from(CategoryRelations::class,"cr_n");
$subNot->andWhere('cr_n.subCategory = c');
$qb = $this->createQueryBuilder('c');
$qb
->select('c')
->where($qb->expr()->exists($subYes->getDQL()))
->andWhere($qb->expr()->not($subNot->getDQL()));
$query = $qb->getQuery();
$DQL = $query->getDQL();
$SQL = $query->getSQL();
$result = $query->getResult();
和错误
[Syntax Error] line 0, col 140: Error: Expected Literal, got 'SELECT'
UPDATE
SELECT c FROM App\Entity\Category c
WHERE EXISTS(SELECT 1
FROM App\Entity\CategoryRelations cr_y WHERE cr_y.mainCategory = c)
AND NOT(SELECT 1 FROM App\Entity\CategoryRelations cr_n WHERE cr_n.subCategory = c)
UPDATE
我内部加入mainCategory和subCategory,但仍然遇到错误
$subYes = $this->getEntityManager()->createQueryBuilder();
$subYes
->select("cr_y")
->from(CategoryRelations::class,"cr_y")
->innerJoin('cr_y.mainCategory', 'cr_ym')
->where($subYes->expr()->eq('cr_ym.id', 'c.id'));
$subNot = $this->getEntityManager()->createQueryBuilder();
$subNot
->select("cr_n")
->from(CategoryRelations::class,"cr_n")
->innerJoin('cr_y.subCategory', 'cr_nm')
->where($subNot->expr()->eq('cr_nm.id', 'c.id'));
$qb = $this->createQueryBuilder('c');
$qb
->select('c')
->where($qb->expr()->exists($subYes->getDQL()))
->andWhere($qb->expr()->not($subNot->getDQL()));
$query = $qb->getQuery();
$DQL = $query->getDQL();
$SQL = $query->getSQL();
$result = $query->getResult();
有我的实体
class Category
/**
* @ORM\OneToMany(targetEntity="CategoryRelations", mappedBy="subCategory")
* @ORM\Cache("NONSTRICT_READ_WRITE")
*/
private $subCategoryRelations;
/**
* @ORM\OneToMany(targetEntity="CategoryRelations", mappedBy="mainCategory")
* @Annotation\Groups({Category::SERIALIZED_GROUP_RELATIONS_LIST})
* @ORM\Cache("NONSTRICT_READ_WRITE")
*/
private $mainCategoryRelations;
以及多对多CategoryRelations
class CategoryRelations
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="subCategoryRelations", cascade={"persist"})
* @Annotation\Groups({Category::SERIALIZED_GROUP_RELATIONS_LIST})
* @ORM\Cache("NONSTRICT_READ_WRITE")
*/
private $subCategory;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="mainCategoryRelations", cascade={"persist"})
* @ORM\Cache("NONSTRICT_READ_WRITE")
*/
private $mainCategory;
您可以使用查询生成器中的“ not()/ in()”方法添加所需的过滤器/约束,并且您的子句看起来像是
$subYes = $this->createQueryBuilder("cr")
->select("cr.mainCategory")
->from("CategoryRelations","cr")
->getDQL();
$subNot = $this->createQueryBuilder("cr")
->select("cr.subCategory")
->from("CategoryRelations","cr")
->getDQL();
$qb = $this->createQueryBuilder("c");
$qb->select("c")
->from("Category", "c");
->where(
$qb->expr()->not(
$qb->expr()->in(
"c.id",
$subNot
)
)
)
->andWhere(
$qb->expr()->in(
"c.id",
$subYes
)
);
$query = $qb->getQuery();
$DQL = $query->getDQL();
$SQL = $query->getSQL();
$result = $query->getResult();
Reference: Doctrine Query Builder nested orX and andX conditions with join