Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Only one case of update
  • Loading branch information
Brice1827 authored Jul 8, 2024
1 parent 7f67625 commit 6292913
Showing 1 changed file with 6 additions and 74 deletions.
80 changes: 6 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,83 +154,15 @@ class CustomerController extends Controller
$modelCustomer = $this->findModel($id);
$modelsAddress = $modelCustomer->addresses;

if ($modelCustomer->load(Yii::$app->request->post())) {

$oldIDs = ArrayHelper::map($modelsAddress, 'id', 'id');
$modelsAddress = Model::createMultiple(Address::classname(), $modelsAddress);
Model::loadMultiple($modelsAddress, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsAddress, 'id', 'id')));

// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelsAddress),
ActiveForm::validate($modelCustomer)
);
}

// validate all models
$valid = $modelCustomer->validate();
$valid = Model::validateMultiple($modelsAddress) && $valid;

if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $modelCustomer->save(false)) {
if (! empty($deletedIDs)) {
Address::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsAddress as $modelAddress) {
$modelAddress->customer_id = $modelCustomer->id;
if (! ($flag = $modelAddress->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $modelCustomer->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}

return $this->render('update', [
'modelCustomer' => $modelCustomer,
'modelsAddress' => (empty($modelsAddress)) ? [new Address] : $modelsAddress
]);
}
}
```

#### 2alt. Alternatively Update Action when primary key of modelsAddress is not "id"
```php
<?php

namespace app\controllers;

use Yii2\Extensions\DynamicForm\Models\Model; //Very important. Do not mix this with yii\base\Model

class CustomerController extends Controller
{
public function actionUpdate($id)
{
$modelCustomer = $this->findModel($id);
$modelsAddress = $modelCustomer->addresses;

// indicate here the real primary key of your model modelCustomer
$pk = "pk_table_modelCustomer";
// primary key of $modelsAddress
$pkey = 'address_id';

if ($modelCustomer->load(Yii::$app->request->post())) {

$oldIDs = ArrayHelper::map($modelsAddress, $pk, $pk);
$modelsAddress = Model::createMultiplePkNotId(Address::classname(), $modelsAddress, $pk);
$oldIDs = ArrayHelper::map($modelsAddress, $pkey, $pkey);
$modelsAddress = Model::createMultiple(Address::classname(), $modelsAddress, $pkey);
Model::loadMultiple($modelsAddress, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsAddress, $pk, $pk)));
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsAddress, $pkey, $pkey)));

// ajax validation
if (Yii::$app->request->isAjax) {
Expand All @@ -250,7 +182,7 @@ class CustomerController extends Controller
try {
if ($flag = $modelCustomer->save(false)) {
if (! empty($deletedIDs)) {
Address::deleteAll([$pk => $deletedIDs]);
Address::deleteAll([$pkey => $deletedIDs]);
}
foreach ($modelsAddress as $modelAddress) {
$modelAddress->customer_id = $modelCustomer->id;
Expand Down

0 comments on commit 6292913

Please sign in to comment.