首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Sylius,按插件搜索产品不起作用

Sylius,按插件搜索产品不起作用
EN

Stack Overflow用户
提问于 2017-12-08 17:14:50
回答 1查看 520关注 0票数 0

我正在尝试按slug过滤产品,使用:

代码语言:javascript
运行
复制
$this->get('sylius.repository.product')->findOneBy(array('slug' => $slug));

我试过使用findBy和findOneBySlug,但总是显示产品没有"slug“属性:

代码语言:javascript
运行
复制
Unrecognized field: slug

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
$product = $repository->findOneBy(array('slug' => 'my-super-product')); // Get one product by defined criteria.
EN

回答 1

Stack Overflow用户

发布于 2017-12-09 18:25:30

我认为这是行不通的,因为slug在产品的翻译中是可用的。存储库中有一些默认方法可能会对您有所帮助,例如:findOneByChannelAndSlugfindByName

或者,您可以在扩展产品存储库时自己构建它:

代码语言:javascript
运行
复制
/**
 * @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()
    ;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47711048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档