Skip to content
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

Draft
wants to merge 3 commits into
base: 4.0
Choose a base branch
from

Conversation

GromNaN
Copy link
Contributor

@GromNaN 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(),
Copy link
Contributor Author

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:

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.

Copy link
Contributor

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,
Copy link
Contributor Author

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:

$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']),
};

Comment on lines 75 to 79
// Virtual field
public function getTitleAttribute(): string
{
return Str::title($this->name);
}
Copy link
Contributor Author

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.

@soyuka
Copy link
Member

soyuka commented Sep 26, 2024

can you run cs-fixer (inside the root of the project)? I'd like to merge this to fix virtual props

@GromNaN
Copy link
Contributor Author

GromNaN commented Sep 26, 2024

I think #6668 does the trick, but I'll leave it to you.

@domthomas-dev
Copy link

I think #6668 does the trick, but I'll leave it to you.

No, with 4.0.2, same error.

@toitzi
Copy link
Contributor

toitzi commented Oct 2, 2024

@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

@domthomas-dev
Copy link

@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 )

@soyuka
Copy link
Member

soyuka commented Oct 2, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants