所以我现在正试图在Doctrine / Symfony中找到一个列的SUM。
目前我有这样的用户以嵌入的形式输入产品(带有与Product(1-m-1)相关联的嵌入式'ProductRecipe'的配方''ProductRecipe.Amount * Product.Unitcost = Productcost',其工作正常。现在我需要通过为每个食谱添加所有'Productcost'来计算'Recipe.recipecost'并将其存储在Recipecost的'Recipe'表中。
我尝试过这个例子http://docs.doctrine-project.org/en/2.1/cookbook/aggregate-fields.html但是终生不能让它发挥作用。我尝试的一切都会引发错误。尝试过预先坚持,坚持,进入和进入。尝试添加新配方时出现的当前错误是:
An exception occurred while executing 'INSERT INTO Recipe (recipename,
recipedesc, recipeyeild, recipecost, created_at, updated_at, u_id) VALUES
(?, ?, ?, ?, ?, ?, ?)' with params ["test cost", "test", 1, null,
"2014-03-22 17:04:56", null, 3]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'recipecost'
cannot be null
很明显,要么计算foreach.productcost要么在持久化之前没有运行,要么根本没有运行。后者是我感觉正在发生的事情,因为即使在尝试运行它之前预先存在它仍然会带来同样的错误。
我目前拥有的是:
//配方实体
class Recipe
{
private $id;
private $recipecost;
protected $product;
public function __construct()
{
$this->product = new \Doctrine\Common\Collections\ArrayCollection();
}
public function setRecipecost($recipecost)
{
$this->recipecost = $recipecost;
return $this;
}
public function getRecipecost()
{
$recipecost = null; // note to self try 0
foreach($this->product AS $productcost){
$recipecost += $productcost->getProduct(); //note to self try getProductcost
}
return $this->recipecost;
}
// ProductRecipe Entity
class ProductRecipe
{
private $id;
private $amount;
private $product;
private $recipe;
private $productcost;
public function setProductcost($productcost) {
$this->productcost = $productcost;
return $this;
}
public function getProductcost() {
return $this->productcost;
}
public function __construct($productcost= 0, $product= '') {
$this->productcost = $productcost;
$this->product = $product;
}
//This function turns Costunit(from Product table) to Productcost.
/**
* @ORM\PrePersist
*/
public function pc2amc() {
$am = $this->amount;
$cu = $this->product->getCostunit();
$productcost = $am * $cu;
$this->productcost = $productcost;
}
我希望我已经覆盖了所有内容,已经删除了我认为无关的大部分代码。如果需要,很乐意发布更多内容。
答案 0 :(得分:1)
解决了我的问题,
此功能:
public function getRecipecost()
{
$recipecost = 0;
foreach($this->product AS $productcost){
$recipecost += $productcost->getProductcost();
}
return $this->recipecost;
}
应该是这样的:
public function setRecipecost() //Set not Get to store in DB
{
// $recipecost = 0; **Remove this line**
foreach($this->product AS $productcost){
$this->recipecost += $productcost->getProductcost(); //$recipecost now $this->recipecost.
}
return $this->recipecost;
}
和Recipe控制器:
public function createAction(Request $request)
{
$entity = new Recipe();
$product = new ProductRecipe(); //* Add this line *
$entity->getProduct()->add($product); //* Add this line *
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity->setUser($this->get('security.context')->getToken()->getUser());
$em->persist($product); //* Add this line *
$em->persist($entity);
$em->flush();
现在是一个梦想,希望这对同一个洞中的任何人都有用。