symfony - 在 Entity SYMFONY 中使用构造函数进行计算
问题描述
我需要在带有 symfony(奏鸣曲)的实体中进行计算,例如:我的实体中有 3 个带有学说的标签,我需要将结果保存在数据库中: $ calcul = $ a + $b;
这是我的实体
class National
{
/**
* @var int
*/
private $id;
/**
* @var string(unique=true)
*/
private $selection;
/**
* @var int
*/
private $a;
/**
* @var int
*/
private $b;
/**
* @var string
*/
private $total;
这是我的经典二传手
/**
* Set a
*
* @param string $a
*
* @return Events
*/
public function setA($a)
{
$this->a = $a;
return $this;
}
/**
* Get a
*
* @return string
*/
public function getA()
{
return $this->a;
}
/**
* Set b
*
* @param string $b
* @return Events
*/
public function setB($b)
{
$this->b = $b;
return $this;
}
所以问题是如何做构造函数???
将 $calcul 中的结果保存在数据库中
(例如:如果我在标签 $a 中写 5,在标签 $b 中写 5 - 我需要在标签 $calcul 中直接写 10 ....)
解决方案
推荐
我不建议将计算数据保存到数据库中(3FN 解释)。
一般来说,您不想存储来自两个或多个其他字段的字段,就好像您需要更新其中一个原始字段一样,这也需要您重新计算重新存储每次结果,每次都需要更多操作。
此外,如果由于某种原因计算必须更改,您将必须更新使用以前的计算完成的所有数据。这将变得非常难以管理。
如果您需要检索两个或更多字段的结果,则应通过在需要的地方调用函数来执行此操作。
技术上
当然,如果您仍然想这样做,或者真的需要它,那么这样做的方法是:
class National {
... // Your fields, getters and setters
public function calculTotal() {
$this->total = $this->a + $this->b;
return $this;
}
}
在您的控制器或服务中
// Fetch you National entity from repository
// Perform the calcul
$national->calculTotal()
// Persist the national entity with the new 'total' fields added to it
$this->entityManager->perist($national);
$this->entityManager->flush();
推荐阅读
- c# - Xamarin 页面导航更改
- javascript - VBA:从 IE 上的组合框中触发选择
- python - Pypy没有名为'encodings' windows 10的模块
- path-finding - 寻路 ascii 网格的轮廓
- python - 在 Python 中使用 Yellowbrick 突出显示 PCA 图中的特定数据点
- r - 使用 for 循环从栅格中提取多个值
- spring-boot - Spring Boot - Web 应用程序可以使用自己的 Rest Api
- docker - 如何理解 LXD 与 VM 和 Docker 的区别?
- flutter - 无法启动 DevTools: TimeoutException after 0:00:10.000000: Future not completed
- scala - 在行中插入值(Spark - Scala)