2017-04-14 46 views
-1

我试图在控制器之外获得原理,但出现了一些问题,我不明白为什么。在控制器之外获得原理

所以我创建了一个服务:

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{ 
} 

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

+0

对不起,我不明白 – user7424312

+0

什么是“出了问题”? – COil

+0

但我怎么能:“调用并注入@ doctrine.orm.entity_manager direclty”? – user7424312

回答

1

你为什么不直接在你的OneLevel类中注入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(); 

     /.../ 
    } 
} 
+1

因为提问者不理解OneLevel服务需要从容器中取出而不是使用新的操作员。他们最终会阅读文档,http://symfony.com/doc/current/service_container.html,并找出它或只是转到不同的框架。 – Cerad

+0

@scoolnico,如果我在OneLevel的构造中还有其他一些参数呢:'public function __construct($ game_name,$ user_id){}'? – user7424312

+0

@ user7424312请编辑您的帖子并解释OneLevel应该做什么。一个实体?控制器?服务? –