Symfony2 getdoctrine outside the model / controller

I am trying to getDoctrine () outside the controller. I created this service:

configurations / services.yml

services:
  update_command:
    class: project\projBundle\Command\Update
    arguments: ['@doctrine.orm.entity_manager']

and in my application /config/config.yml

imports:
    - { resource: "@projectprojBundle/Resources/config/services.yml" }

so is the class I want to use:

namespace project\projBundle\Command;
use Doctrine\ORM\EntityManager;

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

but every time I want to do this: ( Am I doing it right? )

$up = new Update();

I got this error:

Catchable Fatal Error: Argument 1 passed to ...\Update::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in .../Update.php line 7  
+4
source share
3 answers

A simple solution

If you implement the Symfony command (which can be executed on the cron tab), you can access the service container from this command.

<?php
namespace MyProject\MyBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManager;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateCommand extends ContainerAwareCommand
{
    protected $em;

    protected function configure()
    {
        $this->setName('myproject:mybundle:update') ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
    }
}

, . , services.yml.

()

Symfony ( ).

"update" , :

<?php
namespace MyProject\MyBundle\Service;

use Doctrine\ORM\EntityManager;

class MyUpdater
{
    protected $em;

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

    public function runUpdate()
    {
        // All your logic code here
    }
}

services.yml:

services:
    myproject.mybundle.myupdater:
        class: MyProject\MyBundle\Service\MyUpdater
        arguments: ['@doctrine.orm.entity_manager']

:

<?php
namespace MyProject\MyBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('myproject:mybundle:update') ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $myUpdater = $this->getContainer()->get('myproject.mybundle.myupdater');
        $myUpdater->runUpdate();
    }
}
+6

@update_command, , @doctrine.orm.entity_manager.

, . , .

$up = new Update();

ContainerAware, , :

 $up = $this->container->get('update_command');

, update_command, @update_command, .

+2

delete below code in app / config / config.yml, your services.yml will autoload ...

imports:
    - { resource: "@projectprojBundle/Resources/config/services.yml" }

in the new Action, which you can do:

  $up = $this->get('update_command');
0
source

All Articles