Symfony 使用不同的教义2

Symfony 使用不同的教义2,symfony,doctrine-orm,distinct,Symfony,Doctrine Orm,Distinct,我正在用symfony2和doctrine2开发一个应用程序。我创建了一个自定义存储库类,它有一个函数: <?php namespace Anotatzailea\AnotatzaileaBundle\Repository; use Doctrine\ORM\EntityRepository; /** * InterpretatzeaRepository * * This class was generated by the Doctrine ORM. Add your own

我正在用symfony2和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;
    }       

}

粗略地看一下您的存储库方法,它看起来还可以:)IIRC,我认为使用
DISTINCT
很好。如果您确实有问题,您可以随时进行分组

至于在控制器中调用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
注意,您使用名称空间和绑定包的连接版本,后跟冒号和实体;e、 g:
AcmeTestBundle:User


希望这有帮助:)

我该如何使用GROUP BY编写这篇文章@达拉格?谢谢,当然可以。只需将
->groupBy('c.attribute')
添加到createQueryBuilder调用序列的末尾。
DISTINCT
不起作用吗?