2016-09-09 10 views
0

私自身のエンティティのリポジトリをオーバーライドしようとすると少し不満があります。Sylius - カスタムEntityRepositoryを実装する方法

私のエンティティのリストを特別な方法で取得するには、カスタムリポジトリメソッドを作成する必要があります。 1つのqueryBuilderはHavingOrderByです。

質問私の設定を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!")

答えて

3

正しいリポジトリをサブクラス化していません。 ResourceControllerには、Sylius\Component\Resource\Repository\RepositoryInterfaceに基づいたリポジトリが必要です。あなたはDoctrine\ORM\EntityRepositoryからサブクラス化しているので、そうではありません。

あなたのリポジトリはSylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepositoryから継承する(または自分でインターフェイスを実装する)必要があります。

+0

あなたは正しいですSteffen!それは動作します、ありがとうございます。 –

0

私が答えます投稿に正しく応答を貼り付けます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")

主要ポストのエラーが表示されます。その後、最初の投稿が間違っていたconfig.ymlリポジトリの値が設定されていませんでした。

今度は別の時間に設定しましたが、このエラーが発生しました。

混乱して申し訳ありません。

関連する問題