我正在尝试按slug过滤产品,使用:
$this->get('sylius.repository.product')->findOneBy(array('slug' => $slug));我试过使用findBy和findOneBySlug,但总是显示产品没有"slug“属性:
Unrecognized field: slug或
Entity 'Sylius\Component\Core\Model\Product' has no field 'slug'. You can therefore not call 'findOneBySlug' on the entities' repository但他们网站上的文档说它应该是有效的:http://docs.sylius.org/en/latest/components_and_bundles/bundles/SyliusProductBundle/product.html
$product = $repository->findOneBy(array('slug' => 'my-super-product')); // Get one product by defined criteria.发布于 2017-12-09 18:25:30
我认为这是行不通的,因为slug在产品的翻译中是可用的。存储库中有一些默认方法可能会对您有所帮助,例如:findOneByChannelAndSlug或findByName。
或者,您可以在扩展产品存储库时自己构建它:
/**
* @param string $name
* @param string $locale
* @return array
*/
public function findBySlug(string $slug, string $locale): ?ProductInterface
{
return $this->createQueryBuilder('o')
->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
->andWhere('translation.slug = :slug')
->setParameter('slug', $slug)
->setParameter('locale', $locale)
->getQuery()
->getOneOrNullResult()
;
}https://stackoverflow.com/questions/47711048
复制相似问题