0

I am building a batch file using symfony 2 command class. I have a function which deals with DB from a controller inside a bundle

class SubmitDisclosureController extends FOSRestController implements MEAuthController { ... public function discDetails($discId) {
$emr = $this->getDoctrine()->getEntityManager();

I am calling this from a command src/AppBundle/Command/BatchJobCommand.php which is as below

class BatchJobCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
    $output->writeln([
            'User Creator',
            '============',
            '',
        ]);

    // retrieve the argument value using getArgument()
    $output->writeln('First batch job')

    $disc = new SubmitDisclosureController();
    $disc->discDetails('42094');
`

If I try to execute it, it gives PHP Fatal error: Call to a member function has() on null in C:\xampp\htdocs\GR\ vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller. php on line 288

is it not possible to re-use the code by calling the functions of a controller from a command class?

2
  • 2
    Actually what you need is in fact a Service which can be used by both the controller and the command Commented Mar 16, 2017 at 13:37
  • @MateiMihai yes, you are right. But this change will affect existing functions. Commented Mar 16, 2017 at 15:11

2 Answers 2

3
$disc = new SubmitDisclosureController();
$disc->setContainer($this->getContainer());
$disc->discDetails('42094');

Will get you past the error message. However, as @MateiMihai says, a better design would be to move the disc functionality into it's own service and then share it between your controller, command and testing classes. http://symfony.com/doc/current/service_container.html `

Sign up to request clarification or add additional context in comments.

3 Comments

I tried your answer. I am getting a new error. PHP Notice: Use of undefined constant OCI_COMMIT_ON_SUCCESS - assumed 'OCI_COMMIT_ON_SUCCESS'. I am able to connect to DB in local and it is working fine. This issue is only when I try on command line. Is there any separate setting for OCI8 to use in CLI?
Nope. Should be the exact same connection stuff unless you really fooled around with the config files. But you accepted the other answer so I guess all is well.
I accepted the answer because it worked for my original post. Now the issue is with system config I guess Notice: Use of undefined constant OCI_COMMIT_ON_SUCCESS - assumed 'OCI_COMM IT_ON_SUCCESS'
1

What's saying Matei is exactly what you need : create a Service and use it on each class :

class DisclosureService { ...  public function discDetails($discId) {...}  }

In your services.yml configuration file, you have to add it.

 disclosure:
    class:  Your\Namespace\DisclosureService 
    arguments: ["@doctrine", ...]

In your command & controller, you can call the service with :

$this->get('disclosure')->discDetails('')

Details can be found on the official doc : http://symfony.com/doc/current/service_container.html

2 Comments

Note: If you want to use defined services in a command make sure to extend ContainerAwareCommand. Also you need a different code to access the service in the command $this->getContainer()->get('disclosure');. Also see: symfony.com/doc/current/…
Not to nitpick but one of the slightly annoying things about the ContainerAwareCommand and the base Controller class is that the command class does not have a get function. So your code will not work as posted. Edit: Looks like @Joe beat me by about three seconds.

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.