0

symfony2 でアプリケーションを開発し、doctrine2 を使用しています。1 つの関数を持つカスタム リポジトリ クラスを作成しました。

<?php

namespace Anotatzailea\AnotatzaileaBundle\Repository;

use Doctrine\ORM\EntityRepository;

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

    public function getInterpDesberdinak($value)
    {
          $qb = $this->createQueryBuilder('c')
              ->select('DISTINCT c.attribute')
               ->where('c.fer = :Value')
               ->setParameter('Value', $value);
          $Emaitza = $qb->getQuery()->getResult();
          return $Emaitza;
    }       

}

この関数で取得したいのは、個別の c.attribute を持ち、すべて c.fer = 値を持つすべての「Interpretatzea」オブジェクトの配列です。クエリは正しいですか? また、値パラメータをリポジトリ関数に渡す方法も知りたいです。ありがとう

4

1 に答える 1

1

リポジトリメソッドをざっと見てみると、問題ないように見えます:) IIRC、DISTINCTそこの使用は問題ないと思います。問題が発生した場合は、いつでもGROUP BY代わりに a を実行できます。

コントローラーで repo メソッドを呼び出して$value変数を渡すことに関しては、非常に簡単です。例えば:

// in your controller
$value = 'foo';

// get doctrine connection from DI, etc.
$em = $this->getDoctrine()
    ->getEntityManager();

// get the repository object for your 
// entity and call your repository method
$result = $em->getRepository('AnotatzaileaAnotatzaileaBundle:Interpretatzea')
    ->getInterpDesberdinak($value);

// ... do something with your $result here

名前空間とバンドルの連結バージョンを使用し、その後にコロンとエンティティが続くことに注意してください。例えば:AcmeTestBundle:User

お役に立てれば :)

于 2012-05-19T02:43:07.000 に答える