【问题标题】:Symfony DQL query in repository存储库中的 Symfony DQL 查询
【发布时间】:2016-04-14 04:06:47
【问题描述】:

我的查询有问题。我正在 Symfony 2.7 上构建应用程序,我想在存储库中进行查询,但是当我查询时抛出异常:

未定义的方法“getDoctrine”。方法名称必须以 findBy 或 findOneBy 开头!

这是存储库中的代码:

namespace George\ObjectsBundle\Entity;

/**
 * ObjectRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ObjectRepository extends \Doctrine\ORM\EntityRepository
{
public function getOggallery()
{
    $em = $this->getDoctrine()->getManager();
    $query = $this->createQuery("SELECT o, a FROM George\ObjectsBundle\Entity\Object  o JOIN o.ogallery a WHERE a.ord = 0");
    $objects = $query->getResult();

    return $objects;
}

}

但是当我在 Controller 方法中返回代码时,它可以工作。

 $query = $em->createQuery("SELECT o, a FROM George\ObjectsBundle\Entity\Object  o JOIN o.galleries a WHERE a.ord = 0");
 $objects = $query->getResult();

为什么此代码不适用于存储库中的 Doctrine Entity manager?

【问题讨论】:

    标签: symfony doctrine repository


    【解决方案1】:

    您收到此错误是因为您正在调用不存在的存储库方法getDoctrine()。试试这个:

    class ObjectRepository extends \Doctrine\ORM\EntityRepository
    {
        public function getOggallery()
        {
            $em = $this->getEntityManager();
            $query = $em->createQuery("SELECT o, a FROM George\ObjectsBundle\Entity\Object  o JOIN o.ogallery a WHERE a.ord = 0");
            $objects = $query->getResult();
    
            return $objects;
        }    
    }
    

    【讨论】:

    • 它实际上是必需的,因为 EntityRepository 没有 createQuery 方法,而 EntityManager 有。所以这个例子应该是 $em->createQuery(...);甚至 $this->_em->createQuery(...);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    相关资源
    最近更新 更多