I am trying to create small application to read the record from the table by id using symfony/doctrine. My steps:
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".
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.
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.
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.
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).
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)
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