Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method 'getDoctrine'. The method name must start with either findBy or findOneBy

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

like image 257
Rodrigo Díaz R. Avatar asked Sep 30 '15 15:09

Rodrigo Díaz R.


1 Answers

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

like image 100
Dric512 Avatar answered Sep 28 '22 17:09

Dric512