在控制器外获取学说

Get doctrine outside controller

我试图在控制器之外获取条令,但出了点问题,我不明白为什么。

所以我创建了一个服务:

services:
    doctrine.service:
        class: App\DesktopBundle\Lib\DoctrineService
        arguments: [ "@doctrine.orm.entity_manager" ]

和 DoctrineService 文件:

namespace App\DesktopBundle\Lib;

use Doctrine\ORM\EntityManager;

class DoctrineService
{
  protected $manager;

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

而且我想获得此文件中的教义:

namespace App\DesktopBundle\Lib\Game;

use App\DesktopBundle\Entity\OnelevelHistory;
use App\DesktopBundle\Lib\DoctrineService;
use Symfony\Component\Yaml\Yaml;
use Doctrine\ORM\EntityManager;

class OneLevel{
}

但我不知道如何调用之前创建的这个服务。你能帮帮我吗?

你为什么不直接在你的 OneLevel class 中注入 orm 实体管理器?

//services.yml
services:
    one_level.service:
        class: App\DesktopBundle\Lib\Game\OneLevel
        arguments: [ "@doctrine.orm.entity_manager" ]


//App\DesktopBundle\Lib\Game\OneLevel.php
namespace App\DesktopBundle\Lib\Game;

use Doctrine\ORM\EntityManager;
/* Other class you need */

class OneLevel
{
    /* @var Doctrine\ORM\EntityManager $em */
    protected $em;

    /**
     * OneLevel Constructor
     *
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    // The rest of your method ...

    public function exampleMethod()
    {
         /.../

         $this->em->flush();

         /.../
    }
}