backend/controllers/DemandesController.php
/**
     * Creates a new Demandes model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new Demandes model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new Demandes model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new Demandes();
        $modelsEmbranches = [new Embranches];
        if ($model->load(Yii::$app->request->post())) {
            $model->demand_created_date = date('Y-m-d h:m:s');
            $model->demand_update_date = date('Y-m-d h:m:s');
            $modelsEmbranches = Model::createMultiple(Embranches::classname());  
            Model::loadMultiple($modelsEmbranches, Yii::$app->request->post());
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelsEmbranches) && $valid;
            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                   if ($flag = $model->save(false)) {
                        foreach ($modelsEmbranches as $modelEmbranches) {
                            $modelEmbranches->demandes_demand_id = $model->demand_id;
                            $modelEmbranches->embranch_created_date =date('Y-m-d h:m:s');
                            if (! ($flag = $modelEmbranches->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect(['view', 'id' => $model->demand_id]);
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }else{
                 throw new NotFoundHttpException('Embranches insert failer');
            } 
        } else {
            return $this->render('create', [
                'model' => $model,
                'modelsEmbranches' => (empty($modelsEmbranches)) ? [new Embranches] : $modelsEmbranches
            ]);
        }
    }
public function actionCreate ( ) { $ model = new Demandes ( ) ; $ modelsEmbranches = [ new Embranches ] ; if ( $ model -> load ( Yii :: $ app -> request -> post ( ) ) ) { $ model -> demand_created_date = date ( 'Y-m-d h:m:s' ) ; $ model -> demand_update_date = date ( 'Y-m-d h:m:s' ) ; $ modelsEmbranches = Model :: createMultiple ( Embranches :: classname ( ) ) ; Model :: loadMultiple ( $ modelsEmbranches , Yii :: $ app -> request -> post ( ) ) ; $ valid = $ model -> validate ( ) ; $ valid = Model :: validateMultiple ( $ modelsEmbranches ) && $ valid ; if ( $ valid ) { $ transaction = \ Yii :: $ app -> db -> beginTransaction ( ) ; try { if ( $ flag = $ model -> save ( false ) ) { foreach ( $ modelsEmbranches as $ modelEmbranches ) { $ modelEmbranches -> demandes_demand_id = $ model -> demand_id ; $ modelEmbranches -> embranch_created_date = date ( 'Y-m-d h:m:s' ) ; if ( ! ( $ flag = $ modelEmbranches -> save ( false ) ) ) { $ transaction -> rollBack ( ) ; break ; } } } if ( $ flag ) { $ transaction -> commit ( ) ; return $ this -> redirect ( [ 'view' , 'id' => $ model -> demand_id ] ) ; } } catch ( Exception $ e ) { $ transaction -> rollBack ( ) ; } } else { throw new NotFoundHttpException ( 'Embranches insert failer' ) ; } } else { return $ this -> render ( 'create' , [ 'model' => $ model , 'modelsEmbranches' => ( empty ( $ modelsEmbranches ) ) ? [ new Embranches ] : $ modelsEmbranches ] ) ; } }
Creates a new Demandes model.
Creates a new Demandes model .
//$model->save();