unable to access controller functions in command line or console application

54 Views Asked by At

I am preparing a console application and making a connection to database using container of entity manager. This works fine. Code is below

protected function execute(InputInterface $input, OutputInterface $output)
{
    // get Doctrine
    $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
    // find all disc that are not treated by Manager in last week i.e sysdate - 6
    $pendingApprovals = $this->em->getRepository('GMRestBundle:TDtlsDisc')->getPendingManagerAppoval();
    // for each unapproved disc, start sending emails

    $repository = $this->em->getRepository('GMRestBundle:TDtlsDisc');
    $emailReminder = new SubmitDisclosureController();      
    foreach($pendingApprovals as $labManagerReview) {           
        $tDtlsDiscEntity = $repository->findByDiscId($labManagerReview['DISC_ID']);
        $emailReminder->sendMailToLabManager($tDtlsDiscEntity);
    }
}

Now, the issue is it is giving error when I call thsi $emailReminder->sendMailToLabManager($tDtlsDiscEntity); which will in turn makes a connection of orm and get some data from database. This sendMailToLabManager is in SubmitDisclosureController and the code is below.

public function sendMailToLabManager($tDtlsDiscEntity)
{
    $repository = $this->getDoctrine()->getRepository('GMRestBundle:TXrefDiscSso');
    $entityRepository = $this->getDoctrine()->getRepository('GMRestBundle:TDtlsEntity');
    $discId = $tDtlsDiscEntity->getDiscId();
.......

In console application, I am no able to use any controller which access DB by making another connection to doctrine object. Same controller if I called through another action controller in web, it works well.

 Error: Call to a member function has() on null

I see this above error message now.

1

There are 1 best solutions below

0
Roman On

You should create a service which provides the function instead of using the controller. After that you have to insert the EntityManager via dependency injection in to the the service.

If your command extends ContainerAwareCommand you can use $this->get() inside your command to retrieve the service you have defined before.