modules/api/v2/controllers/UserAddressController.php
/**
     * Updates an existing UserAddress model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
Updates an existing UserAddress model.
If update is successful, the browser will be redirected to the 'view' page.
@param integer $id
@return mixed
@throws NotFoundHttpException if the model cannot be found
Updates an existing UserAddress model . If update is successful the browser will be redirected to the ' view ' page . @param integer $id @return mixed @throws NotFoundHttpException if the model cannot be found
public function actionUpdate($id)
    {
        $model = UserAddress::findOne($id);
        if (!$model instanceof UserAddress) {
            throw new NotFoundHttpException('Address doesn\'t exist.');
        }
        $addressData = \Yii::$app->request->post();
        $address['UserAddress'] = $addressData;
        $address['UserAddress']['user_id'] = Yii::$app->user->identity->id;
        if ($model->load($address) && $model->validate()) {
            if (!empty($address['UserAddress']['is_primary_address'])) {
                $model->is_primary_address = UserAddress::IS_ADDRESS_PRIMARY_YES;
            } else {
                $model->is_primary_address = UserAddress::IS_ADDRESS_PRIMARY_NO;
            }
            $model->type = UserAddress::TYPE_BILLING;
            $model->address = $model->street . ', ' . $model->city . ', ' . $model->state . ', ' . $model->country . ' ' . $model->zip_code;
            $model->save(false);
        }
        if ($address['UserAddress']['is_primary_address'] == UserAddress::IS_ADDRESS_PRIMARY_YES) {
            $previousAddress = UserAddress::find()->where(['is_primary_address' => UserAddress::IS_ADDRESS_PRIMARY_YES, 'user_id' => Yii::$app->user->identity->id])->andWhere('id!=' . $model->id)->all();
            if (!empty($previousAddress)) {
                foreach ($previousAddress as $keys => $previousAddressRow) {
                    if (!empty($previousAddressRow) && $previousAddressRow instanceof UserAddress) {
                        $previousAddressRow->is_primary_address = UserAddress::IS_ADDRESS_PRIMARY_NO;
                        $previousAddressRow->save(false);
                    }
                }
            }
        }
        return $model;
    }
public function actionUpdate ( $ id ) { $ model = UserAddress :: findOne ( $ id ) ; if ( ! $ model instanceof UserAddress ) { throw new NotFoundHttpException ( 'Address doesn\'t exist.' ) ; } $ addressData = \ Yii :: $ app -> request -> post ( ) ; $ address [ 'UserAddress' ] = $ addressData ; $ address [ 'UserAddress' ] [ 'user_id' ] = Yii :: $ app -> user -> identity -> id ; if ( $ model -> load ( $ address ) && $ model -> validate ( ) ) { if ( ! empty ( $ address [ 'UserAddress' ] [ 'is_primary_address' ] ) ) { $ model -> is_primary_address = UserAddress :: IS_ADDRESS_PRIMARY_YES ; } else { $ model -> is_primary_address = UserAddress :: IS_ADDRESS_PRIMARY_NO ; } $ model -> type = UserAddress :: TYPE_BILLING ; $ model -> address = $ model -> street . ', ' . $ model -> city . ', ' . $ model -> state . ', ' . $ model -> country . ' ' . $ model -> zip_code ; $ model -> save ( false ) ; } if ( $ address [ 'UserAddress' ] [ 'is_primary_address' ] == UserAddress :: IS_ADDRESS_PRIMARY_YES ) { $ previousAddress = UserAddress :: find ( ) -> where ( [ 'is_primary_address' => UserAddress :: IS_ADDRESS_PRIMARY_YES , 'user_id' => Yii :: $ app -> user -> identity -> id ] ) -> andWhere ( 'id!=' . $ model -> id ) -> all ( ) ; if ( ! empty ( $ previousAddress ) ) { foreach ( $ previousAddress as $ keys => $ previousAddressRow ) { if ( ! empty ( $ previousAddressRow ) && $ previousAddressRow instanceof UserAddress ) { $ previousAddressRow -> is_primary_address = UserAddress :: IS_ADDRESS_PRIMARY_NO ; $ previousAddressRow -> save ( false ) ; } } } } return $ model ; }
Updates an existing UserAddress model.
Updates an existing UserAddress model .