php - Store Multi Dimension array in Yii Create Action -


can 1 suggest me how store array in 2 different table sin yii create action!

array ( [products] => array ( [name] => google [cat_id] => 1 [man_id] => 2 [price] => 40000.00 [model] => model2 [status] => 1 )

[productsattributes] => array     (         [title] => array             (                 [0] => plus+                 [1] => plus                 [2] => maps             )      )  [yt1] => create 

)

in model:

class products extends cactiverecord {     private $_todelete;     private $_attrs; //no $attributes, bc cactiverecord defined , don't want redefine it.     ...      public function setattrs($values)     {         if($this->_attrs && !$this->_todelete)             $this->_todelete = productattributes::model()->findallbyattributes(array('product_id'=>$this->id));         if(!is_array($values))             $values = array('' => $values);         $this->_attrs = $values;     }      public function getattrs()     {         if(!$this->_attrs)         {             if(!$this->isnewrecord)             {                 $productattributes = productattributes::model()->findallbyattributes(array('product_id'=>$this->id));                 foreach($productattributes $productattribute)                     $this->_attrs[$productattribute->name][] = $productattribute->value;             }         }         return $this->_attrs;      }      protected function aftersave()     {         if($this->_attrs && ($this->_todelete || $this->isnewrecord)) //new record or attrs reset         {             foreach($this->_attrs $attribute => $values)             {                 if(!is_array($values)) //fixed. added array check                     $values = array($values);                 foreach($values $value)                 {                      $productattribute = new productattributes();                     $productattribute->name = $attribute;                     $productattribute->value = $value;                     if(!$productattribute->save())                         $this->adderror('attr', 'failed save '.$productattribute->name.': '.$productattribute->value);                 }             }         }         if($this->_todelete)             foreach($this->_todelete $model)                 if(!$model->delete())                     $this->adderror('attr', 'failed delete old '.$model->name.': '$model->value);         return parent::aftersave();     }      ... } 

in controller:

public function actioncreate() {     $products = new products();      if(isset($_post['products']))     {         $products->attributes = $_post['products'];         if(isset($_post['productsattributes']))             $products->attrs = $_post['productsattributes'];         $transaction = yii::app()->db->begintransaction();         if($products->save())         {             if($products->haserrors)                 $transaction->rollback();             else             {                 $transaction->commit();                 ...             }         }     }      ... } 

fixed. added array check


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -