2016-04-24 76 views
0

我試圖重用[@ service_container,@ doctrine.orm.entity_manager]作爲單個變量,但似乎無法弄清楚。我計劃在添加更多模型時重用。Symfony2 yml:將相同的參數傳遞給每個服務

services: 
    generalfunctions: 
     class: classes\classBundle\Controller\DefaultController 
    functionsClass: 
     class: classes\classBundle\Classes\functionsClass  
     arguments: [@service_container,@doctrine.orm.entity_manager] 
    OtakuClass: 
     class: classes\classBundle\Models\otakusModel 
     arguments: [@service_container,@doctrine.orm.entity_manager]  
+0

你可能想看看到[自動連線](http://symfony.com/blog/new-in-symfony-2-8-service-auto-wiring)功能服務也可能是一種可行的方法:http://symfony.com/doc/current/components/dependency_injection/parentservices.html。傳遞容器以及實體管理器似乎很奇怪。 – Federkun

+0

家長

<?php namespace classes\classBundle\Models; use Doctrine\ORM\EntityManager; use Symfony\Component\DependencyInjection\ContainerInterface; class baseModel { protected $container; protected $user; protected $functionsClass; protected $em; protected $connection; public function __construct(ContainerInterface $container) { $this->container = $container; $this->em = $this->container->get("doctrine")->getManager(); $this->connection = $this->container->get('doctrine.dbal.default_connection'); $this->functionsClass = $this->container->get("functionsClass"); $this->user = $this->container->get('security.context')->getToken()->getUser(); } } <?php namespace classes\classBundle\Models; use classes\classBundle\Models\baseModel; use classes\classBundle\Entity\subscriptions; class subscriptionsModel extends baseModel { public function returnSubscription($params) { $repository = $this->em->getRepository('classesclassBundle:subscriptions'); $subscription = $repository->findBy($params); return $subscription; } public function subscribe($params) { $repository = $this->em->getRepository('classesclassBundle:subscriptions'); $params['otakuid'] = $this->container->get("OtakuClass")->getId(); $subscription = $repository->findOneBy($params); if ($subscription == null) { $subscription = new subscriptions(); foreach ($params as $key => $value) $subscription->$key = $value; $this->em->persist($subscription); } else $this->em->remove($subscription); $this->em->flush(); } } services: generalfunctions: class: classes\classBundle\Controller\DefaultController functionsClass: class: classes\classBundle\Classes\functionsClass arguments: [@service_container] OtakuClass: class: classes\classBundle\Models\otakusModel arguments: [@service_container] privateMessagesModel: class: classes\classBundle\Models\privateMessagesModel arguments: [@service_container] subscriptionsModel: class: classes\classBundle\Models\subscriptionsModel arguments: [@service_container] otakusImagesModel: class: classes\classBundle\Models\otakusImagesModel arguments: [@service_container] categoriesModel: class: classes\classBundle\Models\categoriesModel arguments: [@service_container] forumsModel: class: classes\classBundle\Models\forumsModel arguments: [@service_container] 
Cerad

回答

-1

謝謝大家,我最終創建了一個基類,所有的模型都擴展了這個基類。我不知道你可以從服務對象中複製實體管理器。

SY
相關問題