-
-
Notifications
You must be signed in to change notification settings - Fork 882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Laravel] Fix virtual fields metadata #6633
base: 4.0
Are you sure you want to change the base?
Conversation
GromNaN
commented
Sep 20, 2024
Q | A |
---|---|
Branch? | 4.0 |
Tickets | - |
License | MIT |
Doc PR | - |
'default' => null, | ||
'unique' => null, | ||
'fillable' => $model->isFillable($name), | ||
'hidden' => $this->attributeIsHidden($name, $model), | ||
'appended' => $model->hasAppended($name), | ||
'cast' => $cast, | ||
'primary' => $name === $model->getKeyName(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The primary
attribute is accessed for every fields here:
Lines 55 to 56 in 80fc2d2
foreach ($this->modelMetadata->getAttributes($model) as $property) { | |
if (!$property['primary'] && $property['hidden']) { |
In MongoDB, "id" ends as a "virtual field" because we have an accessor for it: https://github.com/mongodb/laravel-mongodb/blob/5.0.2/src/Eloquent/DocumentModel.php#L78-L94
Maybe the metadata should be merged when a field is defined both in database schema and as a virtual field.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might also want to do this on line 83 in the same class and remove the isColumnPrimaryKey
method all along, imho your solution is cleaner.
@@ -131,13 +131,14 @@ public function getVirtualAttributes(Model $model, array $columns): Collection | |||
'name' => $name, | |||
'type' => null, | |||
'increments' => false, | |||
'nullable' => null, | |||
'nullable' => true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The nullable
field must be a boolean, as it is given to the new Type()
here:
core/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php
Lines 73 to 81 in 80fc2d2
$type = match ($builtinType) { | |
'double', 'real' => new Type(Type::BUILTIN_TYPE_FLOAT, $p['nullable']), | |
'datetime', 'date', 'timestamp' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTime::class), | |
'immutable_datetime', 'immutable_date' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable'], \DateTimeImmutable::class), | |
'collection', 'encrypted:collection' => new Type(Type::BUILTIN_TYPE_ITERABLE, $p['nullable'], Collection::class, true), | |
'encrypted:array' => new Type(Type::BUILTIN_TYPE_ARRAY, $p['nullable']), | |
'encrypted:object' => new Type(Type::BUILTIN_TYPE_OBJECT, $p['nullable']), | |
default => new Type(\in_array($builtinType, Type::$builtinTypes, true) ? $builtinType : Type::BUILTIN_TYPE_STRING, $p['nullable']), | |
}; |
// Virtual field | ||
public function getTitleAttribute(): string | ||
{ | ||
return Str::title($this->name); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WIP: Trying to add a test on virtual field.
9c25920
to
d1ea11c
Compare
can you run cs-fixer (inside the root of the project)? I'd like to merge this to fix virtual props |
d1ea11c
to
1897a8b
Compare
I think #6668 does the trick, but I'll leave it to you. |
No, with 4.0.2, same error. |
@domthomas-dev #6668 was not released with 4.0.2, in order to test it you have to apply the patch yourself, i can confirm it is working |
Ok, but ...how ? :/ (I'm sorry, certainely it's a stupid question ) |
I'll tag a release Friday :), @domthomas-dev easiest is to install a dev version that points on the 4.0 branch using composer repositories. Btw, @GromNaN what's left to merge this? |