I am currently working with Symfony2 and I am getting this ERROR message:
Undefined method 'getDoctrine'. The method name must start with either findBy or findOneBy! 500 Internal Server Error - BadMethodCallException
This is my Entity Class:
<?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;
.....
}
This is the Controller's page Action method:
<?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');
}
}
This is my 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();
}
}
I also tried all solutions in this post by ScoRpion and this post by K-Alex
So, Where is THE PROBLEM ???
In the Repository class, instead of:
$em = $this->getDoctrine()->getEntityManager();
You should use:
$em = $this->getEntityManager();
Reference:
http://www.doctrine-project.org/api/orm/2.2/class-Doctrine.ORM.EntityRepository.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With