我只有一个关于查询生成器的简单问题,但我没有找到任何答案......
我为产品列表制作了一个过滤器,我想检查我选择的属性是否在我的 Items(产品项目)的 arrayCollection“属性”中。
这是我在 Item 实体中的 Attributes arrayCollection :
/**
*
* @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'
但我不知道我是否使用好的方法来找到我想要的。