Doctrine orm Doctrine2,使用queryBuilder检查Arraycollection中的实体
我有一个关于查询生成器的简单问题,但我没有找到任何答案 我为产品列表制作了一个过滤器,我想检查我选择的属性是否在我的项目(产品项目)的arrayCollection“attributes”中 以下是Item entity中的my Attributes arrayCollection:Doctrine orm Doctrine2,使用queryBuilder检查Arraycollection中的实体,doctrine-orm,arraycollection,query-builder,Doctrine Orm,Arraycollection,Query Builder,我有一个关于查询生成器的简单问题,但我没有找到任何答案 我为产品列表制作了一个过滤器,我想检查我选择的属性是否在我的项目(产品项目)的arrayCollection“attributes”中 以下是Item entity中的my Attributes arrayCollection: /** * * @var ArrayCollection * * @ORM\ManyToMany(targetEntity="Attribute", inversedBy="items") * @ORM\JoinT
/**
*
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Attribute", inversedBy="items")
* @ORM\JoinTable(name="catalog_items_x_attributes", joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id")}, inverseJoinColumns={@ORM\JoinColumn(name="attribute_id", referencedColumnName="id")})
*/
private $attributes;
以下是我在ProductRepository中的功能(产品有1-x项):
但我不知道我是否用好的方法找到了我想要的
public function listWithFilters($idCat, $filt)
{
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$cat = $em->getRepository('WebformanceCatalogBundle:Category')
->findById($idCat);<br />
$right = $cat[0]->getRight();
$left = $cat[0]->getLeft();
$query = $qb
->select('p')
->from('Webformance\CatalogBundle\Entity\Product', 'p')
->leftJoin('Webformance\CatalogBundle\Entity\Item', 'i', 'WITH', 'i.product = p.id' )
->leftJoin('Webformance\CatalogBundle\Entity\Category', 'c', 'WITH', 'p.category = c.id')
->where('c.right <= :right')
->andWhere('c.left >= :left')
->andWhere($qb->expr()->between('i.salePrice', ':minPrice', ':maxPrice'))
->andWhere($qb->expr()->between('i.weight', ':minWeight', ':maxWeight'))
->setParameters(array('right' => $right,
'left' => $left,
'minPrice' => $filt['price-min'],
'maxPrice' => $filt['price-max'],
'minWeight' => $filt['weight-min'],
'maxWeight' => $filt['weight-max']
));
if (!empty($filt['manufacturer'])) {
$query
->andWhere($qb->expr()->in('p.manufacturer', ':manufacturers'))
->setParameter('manufacturers', $filt['manufacturer']);
}
/***** problem on this part ******/
// I have already try without the "getRepository" just with the ID.
if (!empty($filt['att'])) {
foreach($filt['att'] as $attId) {
$attribute = $em->getRepository('WebformanceCatalogBundle:Attribute')
->findById($attId);
$query
->andWhere($qb->expr()->in(':attributes', 'i.attributes'))
->setParameter('attributes', $attribute[0]);
}
}
/********************************/
$result = $query->getQuery()->getResult();
return $result;
}
[Syntax Error] line 0, col 406: Error: Expected Literal, got 'i'