Sylius:无法将产品列表嵌入视图/自定义存储库
Sylius: Not able to embed a list of products into a view / customize a repository
我想按照文档中的描述将产品列表嵌入到视图中:
http://docs.sylius.org/en/latest/cookbook/embedding-products.html (See also: http://docs.sylius.org/en/latest/customization/repository.html)
我复制并粘贴了上面页面中的大部分代码片段。我使用文档中的代码生成了文件 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
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 也更新了。
我想按照文档中的描述将产品列表嵌入到视图中: http://docs.sylius.org/en/latest/cookbook/embedding-products.html (See also: http://docs.sylius.org/en/latest/customization/repository.html)
我复制并粘贴了上面页面中的大部分代码片段。我使用文档中的代码生成了文件 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
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 也更新了。