gpt4 book ai didi

symfony - 未定义的方法 'getDoctrine' 。方法名称必须以 findBy 或 findOneBy 开头

转载 作者:行者123 更新时间:2023-12-04 21:56:39 34 4
gpt4 key购买 nike

我目前正在使用 Symfony2 并且收到此错误消息:

未定义的方法“getDoctrine”。方法名称必须以 findBy 或 findOneBy 开头!
500 内部服务器错误 - BadMethodCallException

这是我的实体类:

 <?php

namespace Gestionresiduos\ResiduoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="ResiduoRepository")
*/

class BodegaContieneResiduo
{


/**
* @ORM\Id
* @ORM\column(type="integer")
* @ORM\GeneratedValue
*/

protected $idContiene;

.....

}

这是Controller的页面Action方法:
 <?php

namespace Gestionresiduos\ResiduoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
public function portadaAction()
{
$em = $this->getDoctrine()->getEntityManager();

$ofertas= $em->getRepository('ResiduoBundle:BodegaContieneResiduo')->findResiduosAlmacenados();
return $this->render('ResiduoBundle:Default:index.html.twig');
}
}

这是我的 EntityRepository:
    <?php
namespace Gestionresiduos\ResiduoBundle\Entity;

use Doctrine\ORM\EntityRepository;

class ResiduoRepository extends EntityRepository
{

public function findResiduosAlmacenados()
{
$em = $this->getDoctrine()->getEntityManager();
$consulta = $em->createQuery('SELECT r FROM ResiduoBundle:BodegaContieneResiduo');
return $consulta->getOneOrNullResult();
}
}

我也尝试了这个 post 中的所有解决方案通过 ScoRpion 和此 post通过 K-亚历克斯

那么,问题出在哪里???

最佳答案

在 Repository 类中,而不是:

$em = $this->getDoctrine()->getEntityManager();

你应该使用:
$em = $this->getEntityManager();

引用:

http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html

关于symfony - 未定义的方法 'getDoctrine' 。方法名称必须以 findBy 或 findOneBy 开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32869616/

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