0

我正在 symfony2 中开发应用程序并使用教义2。我创建了一个具有一个功能的自定义存储库类:

<?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;
    }       

}

我想用这个函数得到一个包含所有“Interpretatzea”对象的数组,这些对象具有不同的 c.attribute 并且都具有 c.fer = value。查询是否正确?我还想知道如何将 value 参数传递给存储库函数。谢谢

4

1 回答 1

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

Note you use a concatenated version of your namespace and bundle, followed by a colon and the entity; e.g: AcmeTestBundle:User

Hope this helps :)

于 2012-05-19T02:43:07.000 回答