Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted to call method "getDoctrine" on class (Symfony)

I am trying to create small application to read the record from the table by id using symfony/doctrine. My steps:

  1. Created controller (GS\OrderBunndle\Controller\CustomerCuntroller.php), route. Testing URL - it is working;
  2. 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".

like image 202
user4662960 Avatar asked Mar 12 '15 21:03

user4662960


People also ask

Does Symfony work with databases?

Although the Symfony Framework doesn't integrate any component to work with databases, it provides tight integration with a third-party library called Doctrine . Doctrine's sole goal is to give you powerful tools to make database interactions easy and flexible.

What are the common mistakes made by Symfony developers?

One mistake even seasoned developers make when starting a Symfony project is forgetting to set up default charset and collation on their database, ending up with latin type collations, which are default for most databases.

What is doctrine in Symfony?

Doctrine's sole goal is to give you powerful tools to make database interactions easy and flexible. In this chapter, you'll learn how to start leveraging Doctrine in your Symfony projects to give you rich database interactions.

Which version of Symfony is no longer maintained?

Warning: You are browsing the documentation for Symfony 3.4 , which is no longer maintained. Read the updated version of this page for Symfony 6.1 (the current stable version).


1 Answers

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)

like image 186
Tomasz Madeyski Avatar answered Oct 20 '22 16:10

Tomasz Madeyski