Doctrine orm 如何筛选同时具有两个规格值的产品?
我有一个产品实体,它有以下规格:Doctrine orm 如何筛选同时具有两个规格值的产品?,doctrine-orm,query-builder,Doctrine Orm,Query Builder,我有一个产品实体,它有以下规格: /** * @ORM\OneToMany(targetEntity="App\Domain\Entity\Product\ProductSpecification", mappedBy="product", cascade={"all"}) */ private Collection $specifications; 产品规格实体: /** * @ORM\ManyToOne(targetEnti
/**
* @ORM\OneToMany(targetEntity="App\Domain\Entity\Product\ProductSpecification", mappedBy="product", cascade={"all"})
*/
private Collection $specifications;
产品规格实体:
/**
* @ORM\ManyToOne(targetEntity="App\Domain\Entity\Product\Specification\Specification")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private Specification $relatedSpecification;
/**
* @ORM\ManyToMany(targetEntity="App\Domain\Entity\Product\ProductSpecificationValue", cascade="all", inversedBy="specifications")
*/
private Collection $values;
/**
* @ORM\ManyToOne(targetEntity="App\Domain\Entity\Product\Product", inversedBy="specifications")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private ?Product $product = null;
/**
* @ORM\ManyToMany(targetEntity="App\Domain\Entity\Product\ProductCategory")
*/
private Collection $categories;
/**
* @ORM\OneToMany(targetEntity="App\Domain\Entity\Product\Specification\SpecificationTranslation", mappedBy="specification", cascade={"all"})
*/
private Collection $translations;
/**
* @ORM\OneToMany(targetEntity="App\Domain\Entity\Product\Specification\SpecificationValue", mappedBy="specification", cascade={"all"})
*/
private Collection $values;
规范实体:
/**
* @ORM\ManyToOne(targetEntity="App\Domain\Entity\Product\Specification\Specification")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private Specification $relatedSpecification;
/**
* @ORM\ManyToMany(targetEntity="App\Domain\Entity\Product\ProductSpecificationValue", cascade="all", inversedBy="specifications")
*/
private Collection $values;
/**
* @ORM\ManyToOne(targetEntity="App\Domain\Entity\Product\Product", inversedBy="specifications")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private ?Product $product = null;
/**
* @ORM\ManyToMany(targetEntity="App\Domain\Entity\Product\ProductCategory")
*/
private Collection $categories;
/**
* @ORM\OneToMany(targetEntity="App\Domain\Entity\Product\Specification\SpecificationTranslation", mappedBy="specification", cascade={"all"})
*/
private Collection $translations;
/**
* @ORM\OneToMany(targetEntity="App\Domain\Entity\Product\Specification\SpecificationValue", mappedBy="specification", cascade={"all"})
*/
private Collection $values;
SpecificationValue实体:
/**
* @ORM\ManyToOne(targetEntity="App\Domain\Entity\Product\Specification\Specification", inversedBy="values")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private ?Specification $specification = null;
/**
* @ORM\ManyToOne(targetEntity="App\Domain\Entity\Product\Specification\SpecificationValue")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private ?SpecificationValue $relatedSpecificationValue = null;
/**
* @ORM\Column(type="float", nullable=true)
*/
private ?float $priceDifference = null;
/**
* @ORM\Column(type="string", nullable=false)
*/
private string $availability;
ProductSpecificationValue实体:
/**
* @ORM\ManyToOne(targetEntity="App\Domain\Entity\Product\Specification\Specification", inversedBy="values")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private ?Specification $specification = null;
/**
* @ORM\ManyToOne(targetEntity="App\Domain\Entity\Product\Specification\SpecificationValue")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private ?SpecificationValue $relatedSpecificationValue = null;
/**
* @ORM\Column(type="float", nullable=true)
*/
private ?float $priceDifference = null;
/**
* @ORM\Column(type="string", nullable=false)
*/
private string $availability;
目前我得到了以下查询:
public function getListBy(
array $specifications = [],
array $specificationValues = [],
) {
$qb = $this->createQueryBuilder('p');
$qb->leftJoin('p.specifications', 'ps');
$qb->leftJoin('ps.relatedSpecification', 'pss');
$qb->leftJoin('ps.values', 'psv');
$qb->leftJoin('psv.relatedSpecificationValue', 'psrv');
if (count($specifications) > 0) {
$qb->andWhere('pss.id IN (:specifications)');
$qb->setParameter('specifications', $specifications);
}
if (count($specificationValues) > 0) {
$qb->andWhere('psrv.id IN (:specification_values)');
$qb->setParameter('specification_values', $specificationValues);
}
}
如果选择specificationValue A和specificationValue B,则返回specificationValue A或specificationValue B的产品。问题是:如何重写此查询以搜索同时具有specificationValue A和specificationValue B的产品