Sylius - 如何实现自定义 EntityRepository
Sylius - How to implement a custom EntityRepository
尝试覆盖我自己的实体的存储库时,我感到有点沮丧。
我需要创建一个自定义存储库方法以通过特殊方式获取我的实体列表。一个带有 Having
和 OrderBy
.
的 queryBuilder
问题是如何设置我的配置以使用 Sylius,使用我的自定义存储库,而不是默认存储库。
我试试这个:
sylius_resource:
resources:
dinamic.category:
classes:
model: App\Bundle\SyliusBlogBundle\Entity\PostCategory
repository: App\Bundle\SyliusBlogBundle\Repository\PostCategoryRepository
这是我的存储库:
<?php
namespace App\Bundle\SyliusBlogBundle\Repository;
use Doctrine\ORM\EntityRepository;
class PostCategoryRepository extends EntityRepository
{
public function findCategoriesMenu()
{
$queryBuilder = $this->createQueryBuilder('c');
return $queryBuilder
->addSelect('COUNT(p.id) as totalPosts')
->leftJoin('c.posts', 'p')
->andWhere('p.published = true')
->having('totalPosts > 0')
->addGroupBy('p.id')
;
}
}
当我尝试使用此方法时,Symfony 向我抛出此错误:
An exception has been thrown during the rendering of a template ("Undefined method 'findCategoriesMenu'. The method name must start with either findBy or findOneBy!")
我回答 post 以正确粘贴 app/console debug:container dinamic.repository.category
的回复
Information for Service "dinamic.repository.category"
=====================================================
------------------ -------------------------------------------------------------------
Option Value
------------------ -------------------------------------------------------------------
Service ID dinamic.repository.category
Class Dinamic\Bundle\SyliusBlogBundle\Repository\PostCategoryRepository
Tags -
Scope container
Public yes
Synthetic no
Lazy no
Synchronized no
Abstract no
Autowired no
Autowiring Types -
------------------ -------------------------------------------------------------------
到这里就一切正常了
当我尝试访问帖子列表时出现此错误:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 4 passed to Sylius\Bundle\ResourceBundle\Controller\ResourceController::__construct() must implement interface Sylius\Component\Resource\Repository\RepositoryInterface, instance of Dinamic\Bundle\SyliusBlogBundle\Repository\PostCategoryRepository given, called in /Applications/XAMPP/xamppfiles/htdocs/rosasinbox-sylius/app/cache/dev/appDevDebugProjectContainer.php on line 2767 and defined")
未设置存储库配置时出现主要post错误。然后我的第一个 post 是错误的,在 config.yml
上未设置存储库值。
现在我又设置了一次,但出现了这个错误。
抱歉造成混淆。
好吧,您没有对正确的存储库进行子类化。 ResourceController
需要一个基于 Sylius\Component\Resource\Repository\RepositoryInterface
的存储库。由于您是 Doctrine\ORM\EntityRepository
的子类,所以情况并非如此。
您的存储库应继承自 Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
(或自己实现接口)。
尝试覆盖我自己的实体的存储库时,我感到有点沮丧。
我需要创建一个自定义存储库方法以通过特殊方式获取我的实体列表。一个带有 Having
和 OrderBy
.
问题是如何设置我的配置以使用 Sylius,使用我的自定义存储库,而不是默认存储库。
我试试这个:
sylius_resource:
resources:
dinamic.category:
classes:
model: App\Bundle\SyliusBlogBundle\Entity\PostCategory
repository: App\Bundle\SyliusBlogBundle\Repository\PostCategoryRepository
这是我的存储库:
<?php
namespace App\Bundle\SyliusBlogBundle\Repository;
use Doctrine\ORM\EntityRepository;
class PostCategoryRepository extends EntityRepository
{
public function findCategoriesMenu()
{
$queryBuilder = $this->createQueryBuilder('c');
return $queryBuilder
->addSelect('COUNT(p.id) as totalPosts')
->leftJoin('c.posts', 'p')
->andWhere('p.published = true')
->having('totalPosts > 0')
->addGroupBy('p.id')
;
}
}
当我尝试使用此方法时,Symfony 向我抛出此错误:
An exception has been thrown during the rendering of a template ("Undefined method 'findCategoriesMenu'. The method name must start with either findBy or findOneBy!")
我回答 post 以正确粘贴 app/console debug:container dinamic.repository.category
Information for Service "dinamic.repository.category"
=====================================================
------------------ -------------------------------------------------------------------
Option Value
------------------ -------------------------------------------------------------------
Service ID dinamic.repository.category
Class Dinamic\Bundle\SyliusBlogBundle\Repository\PostCategoryRepository
Tags -
Scope container
Public yes
Synthetic no
Lazy no
Synchronized no
Abstract no
Autowired no
Autowiring Types -
------------------ -------------------------------------------------------------------
到这里就一切正常了
当我尝试访问帖子列表时出现此错误:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 4 passed to Sylius\Bundle\ResourceBundle\Controller\ResourceController::__construct() must implement interface Sylius\Component\Resource\Repository\RepositoryInterface, instance of Dinamic\Bundle\SyliusBlogBundle\Repository\PostCategoryRepository given, called in /Applications/XAMPP/xamppfiles/htdocs/rosasinbox-sylius/app/cache/dev/appDevDebugProjectContainer.php on line 2767 and defined")
未设置存储库配置时出现主要post错误。然后我的第一个 post 是错误的,在 config.yml
上未设置存储库值。
现在我又设置了一次,但出现了这个错误。
抱歉造成混淆。
好吧,您没有对正确的存储库进行子类化。 ResourceController
需要一个基于 Sylius\Component\Resource\Repository\RepositoryInterface
的存储库。由于您是 Doctrine\ORM\EntityRepository
的子类,所以情况并非如此。
您的存储库应继承自 Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
(或自己实现接口)。