Symfony 如何在Twig中将基于整数的实体字段转换为人类可读的名称
在Symfony 2.4+项目中,为保存为整数但需要在模板中显示人类可读值的字段注册值数组的最佳方法是什么 我有一个实体,其属性填充了表示不同常量值的整数值:Symfony 如何在Twig中将基于整数的实体字段转换为人类可读的名称,symfony,Symfony,在Symfony 2.4+项目中,为保存为整数但需要在模板中显示人类可读值的字段注册值数组的最佳方法是什么 我有一个实体,其属性填充了表示不同常量值的整数值: /** * The repetition frequency for billing cycle: * @ORM\Column(type="smallint") */ protected $repetition = 0; 我希望将这些值的名称存储在某个位置,因此最初我使用getter将它们放在实体中: protected $rep
/**
* The repetition frequency for billing cycle:
* @ORM\Column(type="smallint")
*/
protected $repetition = 0;
我希望将这些值的名称存储在某个位置,因此最初我使用getter将它们放在实体中:
protected $repetitionName = array(
0 => 'Setup',
1 => 'Second',
2 => 'Minute',
3 => 'Hour',
4 => 'Day',
5 => 'Week',
6 => 'Month',
7 => 'Year'
);
public function getRepetitionName() {
return $this->repetitionName;
}
这似乎是一个伟大的价值观中央存储库
然后在我的小树枝模板中,我不想显示整数,我想要对应的名称值。所以我把它们翻译成这样:
<div class="billingCycle">{{ entity.repetitionName[entity.repetition] }}</div>
$builder->add('repetition', 'choice', array(
'label' => 'Billing Cycle',
'help' => 'The repetition frequency when this service is billed.',
'choices' => $builder->getData()->getRepetitionNamePer(),
// default to monthly (the most common)
'empty_data' => 6,
'required' => TRUE
));
$builder->add('repetition', 'repetition', array(
'label' => 'Billing Cycle',
'help' => 'The repetition frequency when this service is billed.',
// default to monthly (the most common)
'empty_data' => 6,
'required' => TRUE
));
这种方法的问题
一,。翻译:如果我想在任何时候翻译这个,它在一个地方是硬编码的
二,。可重用性:如果我有其他具有重复性的实体,例如事件日历,那么最好重用它
三,。可配置性:理想情况下,它们可以在配置文件中编辑,而不是在实体代码中编辑
替代解决方案:使用配置参数将自定义表单类型作为全局服务
更好的选项似乎是在配置文件中设置一些默认参数:
parameters:
gutensite_component.options.status:
0: Inactive
1: Active
gutensite_component.options.repetition:
0: Setup
1: Second
2: Minute
3: Hour
4: Day
5: Week
6: Month
7: Year
然后创建自定义表单类型Gutensite\ComponentBundle\FormType\RepetitionType,从配置参数加载选项。然后只需参考如下字段类型:
<div class="billingCycle">{{ entity.repetitionName[entity.repetition] }}</div>
$builder->add('repetition', 'choice', array(
'label' => 'Billing Cycle',
'help' => 'The repetition frequency when this service is billed.',
'choices' => $builder->getData()->getRepetitionNamePer(),
// default to monthly (the most common)
'empty_data' => 6,
'required' => TRUE
));
$builder->add('repetition', 'repetition', array(
'label' => 'Billing Cycle',
'help' => 'The repetition frequency when this service is billed.',
// default to monthly (the most common)
'empty_data' => 6,
'required' => TRUE
));
此解决方案中不方便的部分是,您必须在配置中将参数传递给twig,即使您不需要,配置中的twig对于每个模板都会膨胀,或者始终记住从控制器手动将参数传递给twig
// I add to the standard object `$controller->view` which gets passed to Twig
$controller->view->options['repetition'] = $this->container->getParameter('gutensite_component.options.repetition');
访问方式如下:
<div class="priceValue label label-primary">${{ entity.price }}/<span class="billingCycle">{{ view.options.repetition[entity.repetition] }}</span></div>
这是更笨重比我想,但它是可重复使用的。也许其他人有
其他建议?
您是否有其他建议、最佳实践或经验教训?请共享。我要做的是将这些值保存为新实体的对象,并将主实体或任何其他实体的多通关系设置为日历事件实体到重复实体的多通关系 如果是这样,您可以在每次需要或使用任何特定重复获取主实体的所有对象时轻松添加新的重复 此外,您还可以使用输入的人类可读的重复值为主实体的新对象构建一个漂亮的表单:
$builder->add('repetition', 'entity', array(
'class' => 'AcmeDefaultBundle:Repetition', // This is your new 'repetition entity'
'property' => 'repetitionName', // The field of your 'repetition entity' that stores the name (the one that will show a human readable value instead of the id)
'expanded' => true, // True if you prefer checkbox instead of a dropdown list
'label' => 'Billing Cycle',
'help' => 'The repetition frequency when this service is billed.',
'required' => true,
));
现在,当您想在细枝中显示重复的名称时,您将使用:
<div class="billingCycle">{{ entity.Repetition.repetitionName }}</div> (you can also translate that value later)
如果仅对此实体使用,则更简洁的方法是在实体中定义一个公共方法,该方法将返回该数组:
public function getRepetitionNames() {
return array(
0 => 'Setup',
1 => 'Second',
2 => 'Minute',
3 => 'Hour',
4 => 'Day',
5 => 'Week',
6 => 'Month',
7 => 'Year'
);
}
根据重复字段值检索标签的另一种方法:
/**
* @param $key
* @return null
*/
function getRepetitionLabel(){
$repetitions = $this->getRepetitionNames();
return isset($repetitions[$this->repetition]) ? $repetitions[$this->repetition] : 0;
}
现在在twig中访问它会更容易:
<div class="billingCycle">{{ entity.repetitionLabel }}</div>
如果您计划在更多实体中使用它,您可以使用一个接口。只是一个建议:如果您通过转换器传递ID,会怎么样:{{'repetitionName.~entity.repetition}}?当然,如果这个解决方案有效的话,这将允许您轻松地翻译这些字符串course@kix我还没有搞乱翻译,但我会记住这一点。我不确定在这种情况下该如何工作,翻译将存储在何处,和/或是否要将整数转换为人类可读的值。这是一个有趣的建议。getRepetitionLabel函数是一个好主意,但是将这种逻辑添加到不受欢迎的实体中吗?这很方便,但它是否打破了实体应该是什么的原则?当然,实体的目标是处理教义对象…而不是定义这样的静态数组。亚,我喜欢让它在逻辑上成为另一个实体的想法,这是有意义的。但对于一些我认为不需要编辑的内容,例如status=1或0,最好将它们设置为设置,而不是作为另一个每次都需要第二次数据库查询或联接的实体。这是一个复杂的CMS,我真的想限制查询计数。你还会推荐这种方法吗?你说的“限制查询计数”是什么意思?您的数据库是否受限于服务器中的查询?如果不是,我仍然推荐这种方法,使用重复实体。这种方法比使用参数或在主实体中使用数组更简洁,而且即使在不同的项目中也可以重用。此外,您不需要状态字段,因为如果您的主实体在“重复实体”的字段上有一个可为null的属性,您可以知道如果该属性为null,则不会为该用户配置重复,应该询问该用户是否要设置重复。其他一切,在您的控制器和模板中处理这种关系非常容易,而且您永远不需要担心“查询或加入”。您只需要您的userObject,就可以轻松获得重复:{{userObject.getrepeation}}和$userObject->getrepeation;如果$this->repeation为null,repeation getter必须返回一些值,可能为false。正确,框架将为我获取相关实体,而不需要我做任何工作。但它仍然需要调用数据库 获取值。大概这就是存储值的地方,对吗?当数据不需要是动态的时,这是我试图避免的一件事。我认为这是这里的主要考虑因素。但是谢谢你的投入,这是一个很好的选择。