Skip to content

Commit

Permalink
Merge pull request #3 from Brice1827/master
Browse files Browse the repository at this point in the history
Allow for creation of models that have key other than id. Closes #2
  • Loading branch information
mtangoo authored Aug 19, 2024
2 parents a3ff955 + 39da28f commit 73471f0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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').
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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`
Expand Down
9 changes: 5 additions & 4 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ 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();
$post = Yii::$app->request->post($formName);
$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;
}
Expand Down

0 comments on commit 73471f0

Please sign in to comment.