laravel-doctrine / fluent

Fluent mapping driver for Doctrine2
http://www.laraveldoctrine.org/docs/current/fluent
MIT License
43 stars 22 forks source link

Primary Key by Embeddable Value #53

Closed ing200086 closed 6 years ago

ing200086 commented 6 years ago

Is there a way to have part of an embeddable be the primary key for an entity?

A little background in what I am trying to do. I am writing an app which has the data structure like this

object : {
    id => 1
    aliases => [
        'default' => 'bob',
        'proper' => 'robert'
    ]
}

The way the app will work is it allows an entity to have multiple aliases which fulfill multiple alias types. None of the aliases between the different objects can be the same. One object cannot have two aliases with the same alias type.

I think this is a good instance to have an embeddable alias value object, but I am having trouble adding the constraints. I would like the alias value to be the primary key, and have a unique key which is a composite of all the row data.... expecting the rows to look like this

id | alias_type | alias_value 1 default bob 1 proper robert

Am I going about this wrong?

guiwoda commented 6 years ago

Unfonrtunately, no. That's a Doctrine limitation. There are workarounds, by using Types instead of Embeddables, but IMHO it's more trouble than it's worth.

Good luck!

ing200086 commented 6 years ago

Not the response I would love, but a response none the same. Thanks for the response.

Any chance you know of a tutorial or article explaining what you mean by types?

guiwoda commented 6 years ago

This are the official docs on custom mapping types: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/custom-mapping-types.html

I´ve been searching for the discussions regarding Embeddables and primary keys, but I can't find them. I also found a PR that suggest you could do something like this (check the tests), even though the official docs say otherwise. I've been disconnected from the PHP world for a few months and things seem to be moving here!