Sylius:无法将产品列表嵌入视图/自定义存储库

时间:2017-04-23 12:20:11

标签: php symfony sylius

我想将一个产品列表嵌入到视图中,如文档中所述: http://docs.sylius.org/en/latest/cookbook/embedding-products.html(另请参阅:http://docs.sylius.org/en/latest/customization/repository.html

我复制&主要粘贴上面页面中的代码片段。我使用docs中的代码生成了文件src/AppBundle/Repository/ProductRepository.php

<?php

namespace AppBundle\Repository;

use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
use Sylius\Component\Core\Model\ChannelInterface;

class ProductRepository extends BaseProductRepository
{
    /**
     * {@inheritdoc}
     */
    public function findLatestByChannelAndTaxonCode(ChannelInterface $channel, $code, $count)
    {
        return $this->createQueryBuilder('o')
            ->innerJoin('o.channels', 'channel')
            ->addOrderBy('o.createdAt', 'desc')
            ->andWhere('o.enabled = true')
            ->andWhere('channel = :channel')
            ->innerJoin('o.taxons', 'taxon')
            ->andWhere('taxon.code = :code')
            ->setParameter('channel', $channel)
            ->setParameter('code', $code)
            ->setMaxResults($count)
            ->getQuery()
            ->getResult()
        ;
    }
}

然后我在app/config/config.yml中注册了它(再次以1:1复制和粘贴):

sylius_product:
    resources:
        product:
            classes:
                repository: AppBundle\Repository\ProductRepository

我在app/config/routing.yml(1:1复制和粘贴)中配置了路由。

app_shop_partial_product_index_latest_by_taxon_code:
    path: /latest/{code}/{count} # configure a new path that has all the needed variables
    methods: [GET]
    defaults:
        _controller: sylius.controller.product:indexAction # you make a call on the Product Controller's index action
        _sylius:
            template: $template
            repository:
                method: findLatestByChannelAndTaxonCode # here use the new repository method
                arguments:
                    - "expr:service('sylius.context.channel').getChannel()"
                    - $code
                    - $count

然后我希望它在我的index.html.twig中呈现:

<h2 class="ui horizontal section divider header">My custom title</h2>

{{ render(url('app_shop_partial_product_index_latest_by_taxon_code', {'code': 'mugs', 'count': 4, 'template': '@SyliusShop/Product/_horizontalList.html.twig'})) }}

标题是可见的,我正在使用示例数据,因此现有的分类单元代码为“mugs”。但是没有可见的产品清单。

我是否跳过了文档中的内容?我是Symfony的新手,所以也许我忘记了一些明显的东西?我该如何自己调试?

修改:当前版本的文档已过时,请参阅https://github.com/Sylius/Sylius/issues/8212

1 个答案:

答案 0 :(得分:0)

productTaxon有一个新概念,因此正确的功能是:

public function findLatestByChannelAndTaxonCode(ChannelInterface $channel, $code, $count)
        {
            return $this->createQueryBuilder('o')
                ->innerJoin('o.channels', 'channel')
                ->andWhere('o.enabled = true')
                ->andWhere('channel = :channel')
                ->innerJoin('o.productTaxons', 'productTaxons')
                ->addOrderBy('productTaxons.position', 'asc')
                ->innerJoin('productTaxons.taxon', 'taxon')
                ->andWhere('taxon.code = :code')
                ->setParameter('code', $code)
                ->setParameter('channel', $channel)
                ->setMaxResults($count)
                ->getQuery()
                ->getResult();
        }

documentation也已更新。