2

i have one controller which save data in the database i want to run this controller in the crontab. for this I make

 class GetApiController extends Controller {
      public function getapiAction() {
          download xml data..
          saved in database used some querybuilder function.....
           }
    }

now i try to define a console function

  class GetApiCommand extends ContainerAwareCommand
   {

 protected function execute(InputInterface $input, OutputInterface $output)
   {
 $getapicontroller =new GetApiController();  

 $getapicontroller->getapiAction();
   }

   } 

Making instance of this getapi controller and try to call that action method there it give me error that

kBundle\Controller\Controller.php on line 291 Fatal Error: Call to a member function has() on null..

i also try to make this controller service and try to call here with the help of

       $this->forward('app.get_controller:getapiAction');

they give again error that forward is not recognizeable

Any idea how to solve this issue? Thanks in advance for help....

1

1 Answer 1

3

You need to create Service that will do the job you want. Then you can use this service from your Controller and from your Command.

This means that the controller and the console command can both call it, and if you need to change it, you only need to change it in one place.

So if you service has been registered with the name 'my_service', your controller would be something like;

class GetApiController extends Controller {
      public function getapiAction() {
          $my_service = $this->get('my_service');
          $my_service->download();
    }
}

and your console command something like;

<?php
namespace MyBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MyCommandCommand extends ContainerAwareCommand 
{
    ...
    protected function execute(InputInterface $input, OutputInterface $output)
    {
          $myService = $this->getContainer()->get('my_service');
          $my_service->download();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

but doctrain function which i need to use can i use it in this extends ContainerAwareCommand based classes i means query builder etc stuff
Michael can i use doctrain funciton i means the querybuilder etc in the service ???
when you register your service class, use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand to access the class from the console command class
@zahid - Calling a console command from a controller to get a result. Using it controller is not a good idea but it at least gives you an idea about how you can access it from somewhere else.
@rooney. in service is it possiable to use querybuilder doctrain functions?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.