Attempted to call method "getDoctrine" on class (Symfony)
You need to extend Symfony's Controller
class to be able to use getDoctrine()
method.
So:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class CustomerController extends Controller
{
public function indexAction($id)
{
$customer = $this->getDoctrine()->getRepository('GSOrderBundle:Customer')->find($id);
return new Response(
'<html><body>Number: '.$id.'</body></html>'
);
}
}
(you can also define controller as a service but this is more complex solution)
user4662960
Updated on June 04, 2022Comments
-
user4662960 almost 2 years
I am trying to create small application to read the record from the table by id using symfony/doctrine. My steps:
- Created controller (GS\OrderBunndle\Controller\CustomerCuntroller.php), route. Testing URL - it is working;
- Creating Entity for customer table (GS\OrderBundle\Entity\Customer.php) with columns and getters/setters;
3) Created GS\OrderBundle\Entity\CustomerRepository.php (I am not sure why do I need it yet, it was automatically generated). Source code:
namespace GS\OrderBundle\Entity; use Doctrine\ORM\EntityRepository; class CustomerRepository extends EntityRepository { }
4) Modified CustomerController.php:
<?php namespace GS\OrderBundle\Controller; use Symfony\Component\HttpFoundation\Response; use GS\OrderBundle\Entity\Customer; use Doctrine\ORM\EntityManager; class CustomerController { public function indexAction($id) { $customer = $this->getDoctrine()->getRepository('GSOrderBundle:Customer')->find($id); return new Response( '<html><body>Number: '.$id.'</body></html>' ); } }
And getting following error:
Attempted to call method "getDoctrine" on class "GS\OrderBundle\Controller\CustomerController".
500 Internal Server Error - UndefinedMethodException
Stack Trace
in src/GS/OrderBundle/Controller/CustomerController.php at line 13 - public function indexAction($id) { $customer = $this->getDoctrine()->getRepository('GSOrderBundle:Customer')->find($id); return new Response( 'Number: '.$id.'' ); Logs -
1 error INFO - Matched route "customer" (parameters: "_controller": "GS\OrderBundle\Controller\CustomerController::indexAction", "id": "1784", "_route": "customer") DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DumpListener::configure". DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Acme\DemoBundle\EventListener\ControllerListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call method "getDoctrine" on class "GS\OrderBundle\Controller\CustomerController"." at C:\xampp\htdocs\goodstuff\src\GS\OrderBundle\Controller\CustomerController.php line 13 DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DumpListener::configure". DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Acme\DemoBundle\EventListener\ControllerListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController". DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".