gpt4 book ai didi

存储库中的 Symfony DQL 查询

转载 作者:行者123 更新时间:2023-12-02 12:30:54 25 4
gpt4 key购买 nike

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

Undefined method 'getDoctrine'. The method name must start with either findBy or 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 实体管理器一起使用?

最佳答案

您收到此错误是因为您正在调用不存在的存储库方法 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;
}
}

关于存储库中的 Symfony DQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com