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

Detect field type with property type #2661

Open
GromNaN opened this issue Jun 27, 2024 · 5 comments
Open

Detect field type with property type #2661

GromNaN opened this issue Jun 27, 2024 · 5 comments
Labels

Comments

@GromNaN
Copy link
Member

GromNaN commented Jun 27, 2024

Feature Request

Q A
New Feature yes
RFC yes
BC Break no

Summary

In metadata, the default type of fields is string, regardless of the type of field property. I propose to inspect the property types when the field type is not defined. The would only support simple types (maybe with extension point):

property field
int int
string string
float float
DateTime date
DateTimeInterface date_immutable
DateTimeImmutable date_immutable
ObjectId object_id

Benefit: no need to declare the field type.

Usage

#[Document]
class User
{
    #[Id]
    public string $id;

    #[Field]
    public string $name;

    #[Field]
    public DateTimeImmutable $createdAt;

    #[Field]
    public int $level;
}
@IonBazan
Copy link
Member

Sounds useful and I think ORM had it for a while now.
Do you mind if I try to implement this one if you haven't started yet? Otherwise, let me know if you need any help.

@GromNaN
Copy link
Member Author

GromNaN commented Jun 30, 2024

@IonBazan I haven't started investigating yet. Do so if you feel inspired.

@malarzm
Copy link
Member

malarzm commented Jun 30, 2024

We already have that thanks to #2411 and @franmomu :)

/**
* Validates & completes the given field mapping based on typed property.
*
* @psalm-param FieldMappingConfig $mapping
*
* @return FieldMappingConfig
*/
private function validateAndCompleteTypedFieldMapping(array $mapping): array

@GromNaN
Copy link
Member Author

GromNaN commented Jun 30, 2024

We already have that thanks to #2411 and @franmomu :)

Perfect. How could I have missed it despite it being well documented?
I may simplify some doc examples to leverage this feature.

One thing that could be improved is reading the phpdoc for @var Collection<Class> annotation that would indicate the targetDocument of #[EmbedMany] and #[ReferenceMany]. As long as we don't have a native PHP solution for generics.

@IonBazan
Copy link
Member

IonBazan commented Jul 1, 2024

Thanks @malarzm, looks like I forgot about this feature. Perhaps it could be extracted to a separate class as ClassMetadata is already massive. We could also port doctrine/orm#10313 which allows customization of this functionality.

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

No branches or pull requests

3 participants