0

Notification controller

<?php
namespace Main\AdminBundle\Controller;
/* included related namespace */
use Symfony\Component\PropertyAccess\PropertyAccess;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;

use Main\AdminBundle\Entity\Notificationmaster;

class NotificationController extends BaseController
  {
      protected $session;
       protected  $em;

public function __construct(EntityManager $em)
{
    $this->em = $em;
}

/**
 * @Route("/Notification1/{salon_id}",defaults={"salon_id":""})
 * @Template()
 */
public function indexAction($salon_id)
{
        return array("j"=>"jj");
}

/**
 * @Route("/Notification/create/{notification_type}/{notification_title}",defaults={"notification_type":"","notification_title":""})
 */
public function notificationcreateAction($notification_type,$notification_title)
{
    //$this->em = $em;

        $notificationmaster = new Notificationmaster();     
        $notificationmaster->setNotification_type($notification_type);
        $notificationmaster->setNotification_title($notification_title);
        $em = $this->getDoctrine()->getManager();
        $em->persist($notificationmaster);
        $em->flush();           
        return $notificationmaster->getNotification_master_id();    
}   
/**
 * @Route("/Notification/List/{notification_for}/{notification_to}/{lang_id}",defaults={"notification_for":"","notification_to":"","lang_id":""})
 */
 public function notificationlistAction($notification_for,$notification_to,$lang_id)
 {
        //$em = $this->getDoctrine()->getManager();
        return new Response(json_encode("hi")); 

 }
}

in twig file

 {% set notification_html = render(controller('MainAdminBundle:Notification:notificationlist',{"notification_for":"organization","notification_to":"1","lang_id":"1"})) %}

Base Controller

class BaseController extends Controller{

public function __construct()
{
    date_default_timezone_set("Asia/Calcutta");
 }
}

i got this error while i m calling notification list action using Twig file( as above )

Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called

if i remove entity manager , then i got an error in create action Like

Error: Call to a member function has() on a non-object

because i call this create action using this

$notification = new NotificationController($em);
$notification_id = $notification->notificationcreateAction('appointment_book','New  Appointment');

so i have to add entity manager .

Aaska Patel
  • 460
  • 6
  • 21
  • what does your `BaseController` look like? – Jason Roman Feb 04 '16 at 06:48
  • class BaseController extends Controller{ public function __construct() { date_default_timezone_set("Asia/Calcutta"); } } – Aaska Patel Feb 04 '16 at 07:44
  • see my edits for base Controller – Aaska Patel Feb 04 '16 at 07:46
  • where do you call this create action that you are referring to? The one that gives you an error if you remove the entityManager from the constructor... it seems a bit odd to instantiate controllers like that, but can't you just remove the parameter from that instantiation as well if you remove the $em from the constructor? – ejuhjav Feb 04 '16 at 07:50
  • *OR* if you cannot modify that instantiation, you can probably get the twig invocation working by defining you controller as service and injecting the entitymanager in service arguments... – ejuhjav Feb 04 '16 at 08:01
  • yeah you really shouldn't be instantiating the controller at all...you should be grabbing from the service container...unless the Controller that your BaseController extends is not the basic Symfony controller – Jason Roman Feb 04 '16 at 08:03
  • **#ejuhjav** see my edits for create action call – Aaska Patel Feb 04 '16 at 09:08
  • **# Jason Roman** , what should i do for solve this error ? – Aaska Patel Feb 04 '16 at 09:09
  • Unfortunately the updated question still doesn't contain the information where you do that create action call (?) And is there a reason why you couldn't drop the $em parameter from that NotificationController instantiation (i.e. just use $notification = new NotificationController();) ? – ejuhjav Feb 04 '16 at 09:12
  • #ejuhjav -- sorry its my mistake in edit question , i call "createaction" in another controller , by creating object of notification controller – Aaska Patel Feb 04 '16 at 09:17
  • .... and you cannot drop the $em from that instantiation? Just for the record: instantiating controllers from other controller is something that you really shouldn't be doing. – ejuhjav Feb 04 '16 at 09:19
  • then what should i do ? i want call action of another controller in current controller – Aaska Patel Feb 04 '16 at 09:23
  • To address this particular error scenario: you should answer the question whether you can drop the $em from the instantation and whether that will fix this specific issue case. For general symfony structuring: you should move common code that is called from several controllers into services. – ejuhjav Feb 04 '16 at 09:25

1 Answers1

2

First of, to address this particular error case. If you question contains the whole NotificationController, there doesn't seem to be any need to define a constructor nor having the element manager stored in class variable - your notificationcreateAction fetches the entity manager instance from doctrine and doesn't use the class variable. I.e. just remove the constructor and class variable completely:

class NotificationController extends BaseController
{
    protected $session;

    /**
     * @Route("/Notification1/{salon_id}",defaults={"salon_id":""})
     * @Template()
     */
    public function indexAction($salon_id)
    {
        return array("j"=>"jj");
    }

    // and so forth...

And update your the code you use on your controller to:

$notification = new NotificationController();
$notification_id = $notification->notificationcreateAction('appointment_book','New  Appointment');

To answer the question more generically

You shouldn't be instantiating controllers in different controllers. Instead you should create a service for your common code that you can call from all of your controllers. More about symfony services: Service Container

ejuhjav
  • 2,660
  • 2
  • 21
  • 32