Tuesday, September 25, 2012

Do Not Save Null Values in Embedded Form in Symfony

Think there is a Product object with many ProductPrice objects. The Product form contains some ProductPrice forms as embed forms. Prices are not mandatory fields. Now if you save the form, there will be null price rows in the ProductPrice table. Do the following in the ProductPrice model(!) class!
  public function save(Doctrine_Connection $conn = null)
  {
    if($this->price > 0)
    {
      return parent::save();
    }
    else
    {
      $this->delete();
      return null;
    }
  }
This will check the price (there is a price field in the ProductPrice table) is greater than 0 or not. If greater, it will save to the database. If not, it will delete the row in the table (if presents) and stop the saving process for this price object.

No comments:

Post a Comment