Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Simple solution</strong></p> <p>If you're implementing a Symfony command (that can be executed in a cron tab), you can access the service container from the command.</p> <pre><code>&lt;?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-&gt;setName('myproject:mybundle:update') ; } protected function execute(InputInterface $input, OutputInterface $output) { $this-&gt;em = $this-&gt;getContainer()-&gt;get('doctrine.orm.entity_manager'); } } </code></pre> <p>That way, you get the entity manager from a command and don't need to declare this class as a service. You can therefore remove the configuration you added in the <code>services.yml</code> file.</p> <p><strong>An other solution (cleaner)</strong></p> <p>This solution allows better separation of concerns and can therefore be easily unit tested and reused in other parts of your Symfony application (not only as a command).</p> <p>Move all the logic part of your "update" command to a dedicated class that you will declare as a service:</p> <pre><code>&lt;?php namespace MyProject\MyBundle\Service; use Doctrine\ORM\EntityManager; class MyUpdater { protected $em; public function __construct($em) { $this-&gt;em = $em; } public function runUpdate() { // All your logic code here } } </code></pre> <p>Declare it as a service in your <code>services.yml</code> file:</p> <pre><code>services: myproject.mybundle.myupdater: class: MyProject\MyBundle\Service\MyUpdater arguments: ['@doctrine.orm.entity_manager'] </code></pre> <p>Simply call your service from your command :</p> <pre><code>&lt;?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-&gt;setName('myproject:mybundle:update') ; } protected function execute(InputInterface $input, OutputInterface $output) { $myUpdater = $this-&gt;getContainer()-&gt;get('myproject.mybundle.myupdater'); $myUpdater-&gt;runUpdate(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload