From 845cc6cadcc6bf9166df5b5ae3113ee7a11eaad8 Mon Sep 17 00:00:00 2001 From: Brice1827 <61006271+Brice1827@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:52:37 +0200 Subject: [PATCH 1/5] Update Model.php new function createMultiplePkNotId for tables where pk is not 'id' --- src/Models/Model.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Models/Model.php b/src/Models/Model.php index 953754c..564cfcf 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -42,4 +42,40 @@ public static function createMultiple($modelClass, $multipleModels = []) return $models; } + + /** + * Creates and populates a set of models when table primary key is not "id". + * + * @param string $modelClass + * @param array $multipleModels + * @param string $pk + * @return array + */ + public static function createMultiplePkNotId($modelClass, $multipleModels = [], $pk = 'id') + { + $model = new $modelClass; + $formName = $model->formName(); + $post = Yii::$app->request->post($formName); + $models = []; + + if (!empty($multipleModels)) { + $keys = array_keys(ArrayHelper::map($multipleModels, $pk, $pk)); + $multipleModels = array_combine($keys, $multipleModels); + } + + if ($post && is_array($post)) { + foreach ($post as $i => $item) { + if (isset($item['id']) && !empty($item[$pk]) && isset($multipleModels[$item[$pk]])) { + $models[] = $multipleModels[$item[$pk]]; + } else { + $models[] = new $modelClass; + } + } + } + + unset($model, $formName, $post); + + return $models; + } + } From 81b36eef880cf4944eb195504e07712abd1aa0e1 Mon Sep 17 00:00:00 2001 From: Brice1827 <61006271+Brice1827@users.noreply.github.com> Date: Fri, 5 Jul 2024 17:54:19 +0200 Subject: [PATCH 2/5] Update README.md Method for table with pk different from 'id' --- README.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e1eb6a..94475e0 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,78 @@ class CustomerController extends Controller } } ``` - + +#### 2alt. Alternatively Update Action when primary key of modelsAddress is not "id" +```php +findModel($id); + $modelsAddress = $modelCustomer->addresses; + + // indicate here the real primary key of your model modelCustomer + $pk = "pk_table_modelCustomer"; + + if ($modelCustomer->load(Yii::$app->request->post())) { + + $oldIDs = ArrayHelper::map($modelsAddress, $pk, $pk); + $modelsAddress = Model::createMultiplePkNotId(Address::classname(), $modelsAddress, $pk); + Model::loadMultiple($modelsAddress, Yii::$app->request->post()); + $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsAddress, $pk, $pk))); + + // 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([$pk => $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 + ]); + } +} +``` + ### The View The View presents our complex form that will dynamically add or remove items. At the hear of it is the `DynamicFormWidget` From 7f67625788dcb0af7e7203794e2691aa1aee7904 Mon Sep 17 00:00:00 2001 From: Brice1827 <61006271+Brice1827@users.noreply.github.com> Date: Mon, 8 Jul 2024 12:18:00 +0200 Subject: [PATCH 3/5] Update Model.php createLMultiple with an optional paramter pk --- src/Models/Model.php | 39 ++------------------------------------- 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/src/Models/Model.php b/src/Models/Model.php index 564cfcf..e2a5ae2 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -14,44 +14,10 @@ class Model extends \yii\base\Model * * @param string $modelClass * @param array $multipleModels - * @return array - */ - public static function createMultiple($modelClass, $multipleModels = []) - { - $model = new $modelClass; - $formName = $model->formName(); - $post = Yii::$app->request->post($formName); - $models = []; - - if (!empty($multipleModels)) { - $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id')); - $multipleModels = array_combine($keys, $multipleModels); - } - - if ($post && is_array($post)) { - foreach ($post as $i => $item) { - if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) { - $models[] = $multipleModels[$item['id']]; - } else { - $models[] = new $modelClass; - } - } - } - - unset($model, $formName, $post); - - return $models; - } - - /** - * Creates and populates a set of models when table primary key is not "id". - * - * @param string $modelClass - * @param array $multipleModels * @param string $pk * @return array */ - public static function createMultiplePkNotId($modelClass, $multipleModels = [], $pk = 'id') + public static function createMultiple($modelClass, $multipleModels = [], $pk = 'id') { $model = new $modelClass; $formName = $model->formName(); @@ -65,7 +31,7 @@ public static function createMultiplePkNotId($modelClass, $multipleModels = [], if ($post && is_array($post)) { foreach ($post as $i => $item) { - if (isset($item['id']) && !empty($item[$pk]) && isset($multipleModels[$item[$pk]])) { + if (isset($item[$pk]) && !empty($item[$pk]) && isset($multipleModels[$item[$pk]])) { $models[] = $multipleModels[$item[$pk]]; } else { $models[] = new $modelClass; @@ -77,5 +43,4 @@ public static function createMultiplePkNotId($modelClass, $multipleModels = [], return $models; } - } From 6292913348838c46706a86147a8463d66f63607b Mon Sep 17 00:00:00 2001 From: Brice1827 <61006271+Brice1827@users.noreply.github.com> Date: Mon, 8 Jul 2024 12:20:55 +0200 Subject: [PATCH 4/5] Update README.md Only one case of update --- README.md | 80 +++++-------------------------------------------------- 1 file changed, 6 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 94475e0..b78c9cd 100644 --- a/README.md +++ b/README.md @@ -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 -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) { @@ -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; From 39da28f23030a0e511d74ec5b13aa30c45bba505 Mon Sep 17 00:00:00 2001 From: Brice1827 <61006271+Brice1827@users.noreply.github.com> Date: Mon, 19 Aug 2024 14:43:44 +0200 Subject: [PATCH 5/5] Update CHANGELOG.md for issue #2 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2daa100..58f7b56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # yii2-dynamicform change Log +## [v2.0.4 (2024-08-19)](https://github.com/yii2-extensions/dynamicform/compare/v2.0.4...v2.0.3) + +- Bug #2: Fixed Fixed case when primary key is not named "id". + + ## [v2.0.3 (2020-05-18)](https://github.com/yii2-extensions/dynamicform/compare/v2.0.3...v2.0.2) - Enh: Updated composer.json ('symfony/dom-crawler': '~2.8|~3.0' and 'symfony/css-selector': '~2.8|~3.0').