Skip to content

Commit

Permalink
Fixes extracting key values for viaTable relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrogehlen committed Jun 29, 2022
1 parent 84ef416 commit 8e42588
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Yii LinkMany Change Log
=======================

1.1.0 under development
-----------------------

- Fixes extracting key values for `viaTable` relationships

1.1.0 Jan 18, 2022
------------------

Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"config": {
"allow-plugins": {
"yiisoft/yii2-composer": true
}
}
}
11 changes: 10 additions & 1 deletion src/LinkManyBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,16 @@ protected function fillRelation($model, $data, $formName)
protected function extractPrimaryKey($data, $relation)
{
if ($relation->via) {
return $data;
if (is_scalar($data)) {
return $data;
}

$result = [];
foreach ($relation->via->link as $key => $value) {
$result[] = $data[$value];
}

return count($result) === 1 ? $result[0] : $result;
} else {
$modelClass = $relation->modelClass;
$primaryKey = $modelClass::primaryKey();
Expand Down

0 comments on commit 8e42588

Please sign in to comment.