You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using integer custom polymorphic types, Relation::morphMap does not merge them properly because it uses array_merge(), which treats keys differently depending on their type:
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. PHP Documentation.
This difference in behavior may be a problem in some cases. I'll give an example to reproduce the problem.
Before continuing I would like to clarify that I am aware that it would be more appropriate to use string keys and the enum type, but I can not make changes to the project model, 🤷♂️.
Steps To Reproduce:
Imagine you have the following morph map:
// Note that there's no key 0 or 3$morphMap = [
1 => Post::class,
2 => Image::class,
4 => Video::class,
];
Now, if you call to Relation::morphMap($morphMap), the value of Relation::$morphMap would be the following:
But if you try to set the morph map again with new values, as it makes no sense removing the old ones before merging them, array_merge will merge them like this:
By using the array "+" operator there can not be duplicate values. Note that the $map parameter is the left-hand value of the operation so that it is possible to replace values by calling again to Relation::morphMap.
The text was updated successfully, but these errors were encountered:
* upstream/5.5: (84 commits)
Correct docBlock depenency on syncWithoutDetaching (laravel#21478)
update v5.5 changelog
allow to specify the queue while scheduling of queued jobs (laravel#21473)
[5.5] Clear CountQuery "select" bindings since we're overriding the columns anyway (laravel#21468)
add interface
access pivot on resource
update v5.5 changelog
extract trait
Fix docblock in Route (laravel#21454)
fix test
formatting
Fix Relation::morphMap() merge (issue laravel#21457). (laravel#21458)
extract AnonymousResourceCollection into class to allow serialization
revert relationship limits
Fix spelling of 'optionally'
[5.5] Fix Collection::contains() when the found value is null (laravel#21442)
Allow passing a callback to "with" (laravel#21445)
[5.5] Add relation and model attributes in RelationNotFoundException (laravel#21426)
[5.5] Make sure sql for virtual columns is added after the unsigned modifier (laravel#21441)
vendor:publish options alphabetized (laravel#21412)
...
Description:
When using integer custom polymorphic types, Relation::morphMap does not merge them properly because it uses
array_merge()
, which treats keys differently depending on their type:This difference in behavior may be a problem in some cases. I'll give an example to reproduce the problem.
Before continuing I would like to clarify that I am aware that it would be more appropriate to use string keys and the
enum
type, but I can not make changes to the project model, 🤷♂️.Steps To Reproduce:
Imagine you have the following morph map:
Now, if you call to
Relation::morphMap($morphMap)
, the value ofRelation::$morphMap
would be the following:But if you try to set the morph map again with new values, as it makes no sense removing the old ones before merging them,
array_merge
will merge them like this:I think the expected behavior should be the following:
Proposed solution
Since a morph map is an associative array rather than a sequential one, I propose to modify the implementation of
Relation::morphMap
as follows:By using the array "+" operator there can not be duplicate values. Note that the
$map
parameter is the left-hand value of the operation so that it is possible to replace values by calling again toRelation::morphMap
.The text was updated successfully, but these errors were encountered: