backend/controllers/DemandesController.php
/**
     * Updates an existing Demandes model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
Updates an existing Demandes model.
If update is successful, the browser will be redirected to the 'view' page.
@param integer $id
@return mixed
Updates an existing Demandes model . If update is successful the browser will be redirected to the ' view ' page . @param integer $id @return mixed
public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $modelsEmbranches =$this->findEmbranches($model->demand_id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $oldIDs = ArrayHelper::map($modelsEmbranches, 'embranch_id', 'embranch_id');
            $modelsEmbranches = Model::createMultiple(Embranches::classname(), $modelsEmbranches,$id='embranch_id');
            Model::loadMultiple($modelsEmbranches, Yii::$app->request->post());
            $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsEmbranches, 'embranch_id', 'embranch_id')));
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelsEmbranches) && $valid;
            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        if (! empty($deletedIDs)) {
                            Embranches::deleteAll(['embranch_id' => $deletedIDs]);
                        }
                        foreach ($modelsEmbranches as $modelEmbranches) {
                            $modelEmbranches->demandes_demand_id = $model->demand_id;
                            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 {
            return $this->render('update', [
                'model' => $model,
                'modelsEmbranches' => (empty($modelsEmbranches)) ? [new Embranches] : $modelsEmbranches,
            ]);
        }
    }
public function actionUpdate ( $ id ) { $ model = $ this -> findModel ( $ id ) ; $ modelsEmbranches = $ this -> findEmbranches ( $ model -> demand_id ) ; if ( $ model -> load ( Yii :: $ app -> request -> post ( ) ) && $ model -> save ( ) ) { $ oldIDs = ArrayHelper :: map ( $ modelsEmbranches , 'embranch_id' , 'embranch_id' ) ; $ modelsEmbranches = Model :: createMultiple ( Embranches :: classname ( ) , $ modelsEmbranches , $ id = 'embranch_id' ) ; Model :: loadMultiple ( $ modelsEmbranches , Yii :: $ app -> request -> post ( ) ) ; $ deletedIDs = array_diff ( $ oldIDs , array_filter ( ArrayHelper :: map ( $ modelsEmbranches , 'embranch_id' , 'embranch_id' ) ) ) ; $ valid = $ model -> validate ( ) ; $ valid = Model :: validateMultiple ( $ modelsEmbranches ) && $ valid ; if ( $ valid ) { $ transaction = \ Yii :: $ app -> db -> beginTransaction ( ) ; try { if ( $ flag = $ model -> save ( false ) ) { if ( ! empty ( $ deletedIDs ) ) { Embranches :: deleteAll ( [ 'embranch_id' => $ deletedIDs ] ) ; } foreach ( $ modelsEmbranches as $ modelEmbranches ) { $ modelEmbranches -> demandes_demand_id = $ model -> demand_id ; 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 { return $ this -> render ( 'update' , [ 'model' => $ model , 'modelsEmbranches' => ( empty ( $ modelsEmbranches ) ) ? [ new Embranches ] : $ modelsEmbranches , ] ) ; } }
Updates an existing Demandes model.
Updates an existing Demandes model .
// var_dump($modelsEmbranches);exit;