我正在尝试将[@ service_container,@ doctrine.orm.entity_manager]作为单个变量重用,但似乎无法弄明白。我计划在添加更多模型时重复使用。
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 :(得分:0)
你可以:
答案 1 :(得分:-1)
谢谢大家,我最终创建了一个基类,所有模型都扩展了这个基类。我不知道你可以从服务对象中复制实体管理器。
<?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]