2012-05-18 59 views
0

我正在symfony2中開發一個應用程序並使用doctrine2。我創建了一個具有功能的自定義庫類:使用不同的Doctrine2

<?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並且都具有所有的「Interpretatzea」對象的數組c.fer =值。查詢是否正確?我也想知道如何將值參數傳遞給存儲庫功能。謝謝

回答

1

粗略看看你的存儲庫方法表明它看起來好吧:) IIRC,我認爲使用DISTINCT有沒有好。如果你確實有問題,你可以改爲GROUP BY

至於在控制器中調用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

希望這會有所幫助:)

+0

我該如何使用GROUP BY編寫該代碼? @Darragh?謝謝。 – Haritz

+0

當然。只需添加' - > groupBy('c.attribute')'來結束您的createQueryBuilder調用序列。 'DISTINCT'不起作用? –

相關問題