【问题标题】:Sylius, search products by slug not workingSylius,通过 slug 搜索产品不起作用
【发布时间】:2017-12-08 09:14:49
【问题描述】:

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

$this->get('sylius.repository.product')->findOneBy(array('slug' => $slug));

我尝试过使用 findBy 和 findOneBySlug,但它总是说 Product 没有“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.

【问题讨论】:

    标签: symfony sylius


    【解决方案1】:

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

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

    /**
     * @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()
        ;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多