title: SUM of column - Doctrine2 / Symfony2
body: <p>So I'm currently trying to find the SUM of a Column in Doctrine/Symfony.</p>
  
  <p>Currently I have it so a User enters products in an embedded form (Recipe with embedded 'ProductRecipe' associated to Product (1-m-1)) 'ProductRecipe.Amount * Product.Unitcost = Productcost' which works fine as is. Now I need to calculate the 'Recipe.recipecost' by Adding all the 'Productcost' for each recipe and store it in the 'Recipe' table in Recipecost.</p>
  
  <p>I have tried following this example <a href="http://docs.doctrine-project.org/en/2.1/cookbook/aggregate-fields.html" rel="nofollow">http://docs.doctrine-project.org/en/2.1/cookbook/aggregate-fields.html</a> but cannot for the life get it to work. Everything I try throws an error's. Have tried doing it pre-persist, persist, in get and in set. My current error when trying to add a new recipe is this:</p>
  
  <pre><code> 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
  </code></pre>
  
  <p>So obviously either the calculation foreach.productcost isn't either running before persist OR isn't running at all. The latter is what I have a feeling is happening, since even when trying to run it pre-persist it still brings up the same error.</p>
  
  <p>What I currently have is:</p>
  
  <p>//Recipe Entity</p>
  
  <pre><code>class Recipe
  {
      private $id;
      private $recipecost;
      protected $product;
  
      public function __construct()
  {
      $this-&gt;product = new \Doctrine\Common\Collections\ArrayCollection();
  }
  public function setRecipecost($recipecost)
  {
      $this-&gt;recipecost = $recipecost;
      return $this;
  }
  
  public function getRecipecost()
  {
      $recipecost = null; // note to self try 0
      foreach($this-&gt;product AS $productcost){
      $recipecost += $productcost-&gt;getProduct(); //note to self try getProductcost
      }
      return $this-&gt;recipecost;
  }
  </code></pre>
  
  <p>//ProductRecipe Entity</p>
  
  <pre><code>class ProductRecipe
  {
      private $id;
      private $amount;
      private $product;
      private $recipe;
      private $productcost;
  
      public function setProductcost($productcost) {
         $this-&gt;productcost = $productcost;
         return $this;
      }
      public function getProductcost() {
        return $this-&gt;productcost;
      }
  public function __construct($productcost= 0, $product= '') {
      $this-&gt;productcost = $productcost;
      $this-&gt;product = $product;
      }
  
   //This function turns Costunit(from Product table) to Productcost. 
      /**
   * @ORM\PrePersist
   */
  public function pc2amc() {
      $am = $this-&gt;amount;
      $cu = $this-&gt;product-&gt;getCostunit();
      $productcost = $am * $cu;
      $this-&gt;productcost = $productcost;
  }
  </code></pre>
  
  <p>I hope I have covered everything, have ripped out most of the code I think is unrelated. Happy to post more if need be. </p>