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'). diff --git a/README.md b/README.md index 5e1eb6a..b78c9cd 100644 --- a/README.md +++ b/README.md @@ -154,12 +154,15 @@ class CustomerController extends Controller $modelCustomer = $this->findModel($id); $modelsAddress = $modelCustomer->addresses; + // primary key of $modelsAddress + $pkey = 'address_id'; + if ($modelCustomer->load(Yii::$app->request->post())) { - $oldIDs = ArrayHelper::map($modelsAddress, 'id', 'id'); - $modelsAddress = Model::createMultiple(Address::classname(), $modelsAddress); + $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, 'id', 'id'))); + $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsAddress, $pkey, $pkey))); // ajax validation if (Yii::$app->request->isAjax) { @@ -179,7 +182,7 @@ class CustomerController extends Controller try { if ($flag = $modelCustomer->save(false)) { if (! empty($deletedIDs)) { - Address::deleteAll(['id' => $deletedIDs]); + Address::deleteAll([$pkey => $deletedIDs]); } foreach ($modelsAddress as $modelAddress) { $modelAddress->customer_id = $modelCustomer->id; @@ -206,7 +209,7 @@ class CustomerController extends Controller } } ``` - + ### The View The View presents our complex form that will dynamically add or remove items. At the hear of it is the `DynamicFormWidget` diff --git a/src/Models/Model.php b/src/Models/Model.php index 953754c..e2a5ae2 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -14,9 +14,10 @@ class Model extends \yii\base\Model * * @param string $modelClass * @param array $multipleModels + * @param string $pk * @return array */ - public static function createMultiple($modelClass, $multipleModels = []) + public static function createMultiple($modelClass, $multipleModels = [], $pk = 'id') { $model = new $modelClass; $formName = $model->formName(); @@ -24,14 +25,14 @@ public static function createMultiple($modelClass, $multipleModels = []) $models = []; if (!empty($multipleModels)) { - $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id')); + $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['id']) && isset($multipleModels[$item['id']])) { - $models[] = $multipleModels[$item['id']]; + if (isset($item[$pk]) && !empty($item[$pk]) && isset($multipleModels[$item[$pk]])) { + $models[] = $multipleModels[$item[$pk]]; } else { $models[] = new $modelClass; }