2011-11-16 7 views
31

symfony2.Iで新しくなったのは、コマンドラインでエンティティクラスを作成したときにリポジトリクラスを作成したのですが、そのリポジトリクラスのカスタム関数にアクセスできませんでした。 symfony2でカスタムリポジトリクラスを作成する方法は?誰も私にいくつかのサンプルコードで最初からステップの説明でステップを与えることができますか?以下は symfony2のカスタムリポジトリクラス

は私が

Undefined method 'findAllOrderedByName'. The method name must start with either findBy or findOneBy! 

は、私は間違いを持っ​​ていますか、以下のエラーを得た

$em = $this->getDoctrine()->getEntityManager(); 
      $pro = $em->getRepository('symBundle:Register') 
      ->findAllOrderedByName(); 

この方法のように私のコントローラで呼び出さ

namespace Mypro\symBundle\Entity; 

use Doctrine\ORM\EntityRepository; 

/** 
* RegisterRepository 
* This class was generated by the Doctrine ORM. Add your own custom 
* repository methods below. 
*/ 
class RegisterRepository extends EntityRepository 
{ 


    public function findAllOrderedByName() 
    { 
     return $this->getEntityManager() 
      ->createQuery('SELECT p FROM symBundle:Register p ORDER BY p.name ASC') 
      ->getResult(); 
    } 


} 

私のリポジトリクラスである私コード?リポジトリクラスを作成する際に間違いがありますか?私はどんなクラスを使う必要がありましたか?

答えて

68

あなたのエンティティにこのリポジトリを登録するのを忘れたと思います。 エンティティ設定ファイルにリポジトリクラスを追加するだけです。 SRC/Mypro/symBundle /リソース/設定/ドクトリン/ Register.orm.ymlで

Mypro\symBundle\Entity\Register: 
    type: entity 
    repositoryClass: Mypro\symBundle\Entity\RegisterRepository 

は、念のために、この変更後にキャッシュをクリアすることを忘れないでください。

そして、あなたが(代わりにYMLの設定の)注釈を使用している場合、代わりに上記のようなものを追加:リポジトリを登録するには、あなたのエンティティクラスに

+0

Reuven:

<entity name="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContent" table="uv_update_page_content" repository-class="Ccd\Bundle\FrontendBundle\Entity\UvUpdatePageContentRepository"> 

http://doctrine-mongodb-odm.readthedocs.org/en/latest/cookbook/mapping-classes-to-orm-and-odm.html その後、キャッシュを更新しますか?あなたはそれを知っていますか? –

+0

ネットビーンがカスタムリポジトリを知ることは考えられません。私のバージョンのネットビーンは、symfony固有のコードを自動補完しません。 – Reuven

+0

同じ問題があり、SymfonyでEntityRepositoryを生成しましたが、src/Projet/Bundle/Resources/config /に "doctrine"というフォルダがありません。何が問題なのですか? – httpete

6

/** 
* @ORM\Entity(repositoryClass="Mypro\symBundle\Entity\RegisterRepository") 
*/ 

をマニュアルには、Aはステップガイドによって素敵なステップ... http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

  • まず注釈/ YAML設定にリポジトリクラスを定義
  • そして、あなたは...あなたがで順序を設定できることを行うために、カスタムレポを必要としないすべての
+0

にrepository-class = "your \ Bundle \ Entity \ ProductRepository"を追加してください。注釈を '@ORM \ Entity(repositoryClass =" Acme \ StoreBundle \ Repository \ ProductRepository ")から' @ORM \ Entity(repositoryClass = ")のように変更しない限り、Manaulの例はSymfony 2.1.1では動作しません。 Acme \ StoreBundle \ Entity \ ProductRepository ")' Entity \ Product.phpファイルのネームスペースで始まる – yitwail

2

まず....新しく作成された関数を呼び出す機能

  • を作成し、クラス
  • を作成します。 EM getRepository findBy方法で句:

    //$em - entity manager 
    //from Doctrine documentation: findBy(criteria(array), order(array), limit, offset) 
    $result = $em->getRepository('symBundle:Register')->findBy(array(), array('name' => 'ASC')) 
    
  • 4

    私はコンフィギュレーションのこの混乱に入ったとき、私はあまりにも多くの時間を失いました。私は、Entity内のリポジトリクラスをアノテーションマッピングとして設定していました。マッピングはそこにありましたが、依然としてリポジトリはエンティティに関連付けられていませんでした。注釈マッピング、すなわち @ORM \ Entity(repositoryClass = "Acme \ DemoBundle \ Entity \ UserRepository")を最後の行に移動すると、その行が機能しました。

    /* 
    * User 
    * 
    * @ORM\Table() 
    * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\UserRepository") 
    */ 
    class User 
    { 
    ... 
    }` 
    
    +0

    奇妙なことに、最後の2時間を費やしても問題は解決しました。 –

    2

    私は単にネイティブドキュメントdocに従うと、私が選択した注釈を探します。たとえば、yamlを選択し、Product.orm.ymlとCategory.ormに設定を追加しました。また、新しいPHPは、エンティティのメソッドに属性を追加YMLファイル:成功した新しいゲッターを追加

    Category.phpため
    protected $products; 
    
    public function __construct() 
    { 
        $this->products = new ArrayCollection(); 
    } 
    

    とProduct.php

    ため

    protected $category; 
    

    その後、php app/console doctrine:generate:entities Acme を実行し、セッター

    4

    マッピング用のxml: フォルダ内のxmlマッピングファイルを更新するResource/config/doctrine、ad Dリポジトリ-class属性:私は正しい出力を得たが、私にNetBeans IDEで私のカスタムリポジトリfunction.yを検出することができませんでした

    php app/console doctrine:cache:clear-metadata 
    php app/console cache:clear