public function actionStructSave($id) { $struct = new DishStructure(); $halfStruct = new DishStructure2(); $prod_id = CHtml::listData(DishStructure::model()->findAll(array("condition" => "dish_id = {$id}")), 'struct_id', 'prod_id'); $stuff_id = CHtml::listData(DishStructure2::model()->findAll(array("condition" => "dish_id = {$id}")), 'struct2_id', 'halfstuff_id'); $this->chosenProduct = CHtml::listData(Products::model()->findAllByPk($prod_id), 'product_id', 'name'); $this->chosenHalfstuff = CHtml::listData(Halfstaff::model()->findAllByPk($stuff_id), 'halfstuff_id', 'name'); if ($_POST) { $transaction = Yii::app()->db->beginTransaction(); try { if ($_POST['prod']) { foreach ($_POST['prod'] as $key => $val) { $tempStruct = DishStructure::model()->updateAll(array('amount' => $val), 'dish_id = :dish_id AND prod_id = :prod_id', array(':dish_id' => $id, ':prod_id' => $key)); } } if ($_POST['half']) { foreach ($_POST['half'] as $key => $val) { $tempStruct = DishStructure2::model()->updateAll(array('amount' => $val), 'dish_id = :dish_id AND halfstuff_id = :halfstuff_id', array(':dish_id' => $id, ':halfstuff_id' => $key)); } } $transaction->commit(); Yii::app()->user->setFlash($messageType, $message); $this->redirect(array('index')); } catch (exception $ex) { $transaction->rollBack(); Yii::app()->user->setFlash('error', "{$e->getMessage()}"); } } $this->render('structSave', array()); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return DishStructure the loaded model * @throws CHttpException */ public function loadModel($id) { $model = DishStructure::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }