public function actionCreate() { if (Yii::app()->request->getUrlReferrer() !== Yii::app()->createAbsoluteUrl(Yii::app()->request->getUrl())) { Yii::app()->session->offsetUnset($this->id . '_bankList'); Yii::app()->session->offsetUnset($this->id . '_Societes'); } $model = $this->loadModel(); $banqueModel = new BanqueForm(); if (null !== ($formData = Yii::app()->request->getPost(get_class($model)))) { $model->setAttributes($formData); if (Yii::app()->request->isAjaxRequest) { echo CActiveForm::validate($model); Yii::app()->end(); } if ($model->save()) { if (Yii::app()->session->offsetExists($this->id . '_bankList')) { foreach (Yii::app()->session->offsetGet($this->id . '_bankList') as $attributes) { $attributes['societe_id'] = $model->primaryKey; $banqueModel = new Banque(); $banqueModel->setAttributes(array_merge($attributes, array('societe_id' => $model->primaryKey))); $banqueModel->save(false); } Yii::app()->session->offsetUnset($this->id . '_bankList'); } $this->redirect(array('view', 'id' => $model->primaryKey)); } } $this->render('create', array('model' => $model, 'banqueModel' => $banqueModel, 'bankList' => $this->getBankDataProvider($model->primaryKey))); }
/** * Load Banque model * @param integer $id * @return Banque * @throws CHttpException */ protected function loadModel($id = null) { if (null === $id) { $model = new Banque(); } else { $model = Banque::model()->findByPk($id); } if ($model instanceof CModel) { $model->titleField = 'beneficiaire'; return $model; } throw new CHttpException(404, 'Item not found'); }