请帮助,我有一个表orders与total_price,exchange_code (它包含货币代码如美元),exchange_rate (包含货币汇率类似: 1.7)。
现在,我想创建一个虚拟字段“exchangedTotalPrice”,它将包含exchangedTotalPrice= exchangeRate * totalPrice。
我不想在数据库中创建单独的列,因为我的db表中已经有太多列,我的要求是创建更多的虚拟字段。
如果您需要什么或不理解我的查询,请评论。
发布于 2017-04-17 08:14:55
您可以使用特定的方法来提供所需的内容(如注释中所述)。
public function getExchangedTotalPrice()
{
// do the maths and return the result.
$result = $this->exchangeRate * $this->totalPrice;
return $result;
}有趣的是,你可以用表单或其他许多地方来连接它。
表单
例如,如果您有一个表单,其构建器如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
//... some other builder stuff here
$builder->add('exchangedTotalPrice', TextType::class, [
'mapped' => false, // this will stop the setter being called on submit
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'allow_extra_fields' => true, // youll need this to allow the extra field element
]);
}当symfony试图填充表单时,它将尝试根据字段的名称调用一个getter和setter。因此,接下来将使用上面定义的方法。
Twig
同样的情况也会发生在树枝上。
{{ Orders.exchangedTotalPrice }}这也将调用新字段上的getter。
我没有测试这些,所以您可能需要进行调试。
https://stackoverflow.com/questions/43447023
复制相似问题