cakephp set values to model before saving form data -
i'm learning cakephp. want submit form data database , before saving, wish modify few fields. here tried:
public function addproduct() { $this->layout = false; $this->render ( false ); $this->loadmodel ( 'product' ); $this->product->create(); $conditions = array('product.category_id' => $this->request->data["categoryproduct"], 'product.company_id' => $this->request->data["companyproduct"], 'product.name' => $this->request->data["name"] ); $product = $this->product->find ( 'first', array ('conditions' => $conditions ) ); if ($product) echo "duplicate"; else{ $discount = $this->request->data["discount"]; if($discount>0){ $cost = $this->request->data["cost"]; $this->product->costforyou = intval($cost - $cost * $discount / 100); } else $this->product->costforyou = 0; $this->product->category_id = $this->request->data["categoryproduct"]; $this->product->company_id = $this->request->data["companyproduct"]; $this->product->create_date = date('y-m-d h:i:s'); $this->product->status = "active"; if ($this->product->save($this->request->data)) { echo "product added"; } else { echo "error in adding product"; } } }
but fields i'm setting manually not getting data. tried have @ cakephp books couldn't find there.
i worked out. here new code:
public function addproduct() { $this->layout = false; $this->render ( false ); $this->loadmodel ( 'product' ); $this->product->create(); $conditions = array('product.category_id' => $this->request->data["categoryproduct"], 'product.company_id' => $this->request->data["companyproduct"], 'product.name' => $this->request->data["name"] ); $product = $this->product->find ( 'first', array ('conditions' => $conditions ) ); if ($product) echo "duplicate"; else{ $discount = $this->request->data["discount"]; $costforyou = 0; if($discount>0){ $cost = $this->request->data["cost"]; $costforyou = intval($cost - $cost * $discount / 100); } $this->request->data["category_id"] = $this->request->data["categoryproduct"]; $this->request->data["company_id"] = $this->request->data["companyproduct"]; $this->request->data["costforyou"] = $costforyou; $this->request->data["createdate"] = date('y-m-d h:i:s'); $this->request->data["status"] = "active"; if ($this->product->save($this->request->data)) { echo "product added"; } else { echo "error in adding product"; } } }
Comments
Post a Comment