昨天,我在IRC Symfony2频道上讨论了最佳实践方法和教程(书,sf2-网站上的食谱)。
我的配置服务的命名目前有点糟糕,但目前我没有任何策略或良好的命名概念。
目前,我在控制器中调用$this->get('test.conf')->getCategory('data-browser')。这样做“感觉很好”,因为我不需要在控制器中重复这个函数。然后,服务test.conf调用工厂服务test.conf_factory,该服务获取service.configuration的实例。
问题:
test.conf_factory、test.conf和test.configuration吗?目前我真的不知道我做得对不对。test.configuration并创建另一个服务(它只是在构造函数中创建一个test.configuration实例并通过方法'get‘实例的调用返回)是否更好(或者说是可能的)?(injection/introduction.html)是否有像大多数symfony开发人员那样被证明的方法?配置工厂:
namespace Test\CoreBundle\Factory;
use Test\CoreBundle\Entity\Account;
use Test\CoreBundle\Service\Configuration;
class ConfigurationFactory
{
private $user;
private $configuration;
public function __construct(Account $user = null, Configuration $configuration)
{
$this->user = $user;
dump($user);
$this->configuration = $configuration;
}
public function get()
{
if ($this->user) {
// dump($this->user);
$this->configuration->setAccountid($this->user->getId());
}
return $this->configuration;
}
}配置序列化:
<?php
namespace test\CoreBundle\Service;
class Configuration {
private $em;
private $category;
private $accountid;
/**
* Values from database
* @var array
*/
private $ValuesFromDB = null;
public function __construct($em)
{
$this->em = $em->getRepository('TestCoreBundle:Conf');
}
public function setAccountid($accountid) {
$this->accountid = $accountid;
$this->loadAll();
}
public function loadAll() {
$DBconf = $this->em->findAllByAccount($this->accountid);
foreach($DBconf as $key => $conf) {
$values[$conf->getCategory()][$conf->getKey()] = $conf->getValue();
}
$this->ValuesFromDB = $values;
}
public function setCategory($category) {
$this->category = $category;
}
public function getCategory($category) {
return $this->ValuesFromDB[$category];
}
public function get($key) {
return $this->ValuesFromDB[$this->category][$key];
}
}services:
test.account_factory:
class: test\CoreBundle\Factory\AccountFactory
arguments: [@security.context]
test.account:
class: test\CoreBundle\Entity\Account
factory_service: test.account_factory
factory_method: get
test.configuration:
class: test\CoreBundle\Service\Configuration
arguments: [@doctrine.orm.entity_manager]
test.conf_factory:
class: test\CoreBundle\Factory\ConfigurationFactory
arguments:
- @test.account
- @test.configuration
test.conf:
class: Configuration
factory_service: test.conf_factory
factory_method: get发布于 2015-05-25 17:57:16
我不确定我是否非常了解您的设计问题,但我想您实际上要做的是根据特定的实体(这里是您的用户)从数据库中检索一些数据。那我就试着和你分享我的看法:
我从你的设计中看到的是,你误解了工厂的概念:在这里,它显然是无用的。
然后,我会试图通过思考:我的工具应该做什么来恢复你的工具?你不认为你正在实现的设计太复杂了吗?
如果我理解你想要做好什么,我想你可以通过以下方式使用你的工具:
在你的帖子开始时,你的问题实际上是不应该被问到的,因为你不应该在你的课堂上注入一个帐户实例。最后,就像这样:
// I get my account
$myAccount = $this->getEntityManager()->getRepository('TestCoreBundle:Conf')->find(/* ... */);
$configurationManager = $this->get('configuration_manager');
// The manager permits me to get the "extra stuff" related to this account.
$confStuff = $configurationManager->getConfigurationStuffFromAccount($myAccount);..。如果我能理解的话,应该是你解决问题的方法。
这条路:
https://stackoverflow.com/questions/30273873
复制相似问题