php - Problems with doctrine in Symfony controller -


i have error can find no solution. have driver symfony this:

<?php  namespace consolidador\panelbundle\controller;  use symfony\bundle\frameworkbundle\controller\controller;  class datacontroller extends controller {      public function listofusers()     {         $em = $this->getdoctrine()->getmanager();         $settings = $em->getrepository('panelbundle:users')->findall();          return $settings;      }  } 

as seen returns list of users using doctrine, when call on method, this:

    $users = new datacontroller();     $listofusers = $users->listofusers() 

an exception appears in datacontroller class has doctrine:

error: call member function has() on non-object

i not understand happens , i'm using , extending controller should able access doctrine without error ... in other controllers use doctrine without major problems.

any solution?

thank much.

this because way create instance of class. regular symfony controller instance created service container calls setcontainer method on it. datacontroller class has no knowledge container , that's why error (you can't new datacontroller()).

a quick , dirty way solve set container on datacontroller instance:

$users = new datacontroller(); $users->setcontainer($container); //you need have container available, don't know context in $listofusers = $users->listofusers(); 

a proper way define controller service, inject doctrine , by

$datacontroller = $this->container->get('your_data_controller_class'); 

important note: other thing controller meant take request, , return response object, in case returning array not good. should move logic somewhere outside controller class - usermanager class. should define usermanager service, inject doctrine, logic there , return data (an array). in controller should call service.

this way cleaner code , move logic outside of controller.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -