Skip to content

Loading…

DDC-2987: Possibility to use a field / fields from an Embeddable as primary key(s) #3751

Closed
doctrinebot opened this Issue · 3 comments

2 participants

@doctrinebot

Jira issue originally created by user antonstoeckl:

Hi there,

I'm using the brand new embedded objects in doctrine ORM in a DDD application. It's so great that you guys added this feature which enables clean DDD Value Objects!

What does not seem to work so far is using an embeddable as primary key.
I have an enbeddable "CarId" which has only one field "value" representing the uuid for the parent entity "Car".
I tried to get this to work with different approaches, but with no success.

I'm able to define the primary key in the embeddable itself, sample .yml:

*my_namespace*\CarEntity:
    type: entity
    table: car
    repositoryClass: *my_namespace*\ConcreteCarRepository
    fields:
        name:
            type: string
            length: 100
    embedded:
        uuid:
            class: CarUuidValue
        model:
            class: CarModelValue

*my_namespace*\CarIdValue:
    type: embeddable
    id:
        value:
            type: string
            length: 36

This ends up with a valid entity, but the field holding the id is named id_id. Setting an empty columnPrefix does not work:

    /****
     * Inline the embeddable class
     *
     * @param string $property
     * @param ClassMetadataInfo $embeddable
     */
    public function inlineEmbeddable($property, ClassMetadataInfo $embeddable)
    {
        foreach ($embeddable->fieldMappings as $fieldMapping) {
            $fieldMapping['declaredField'] = $property;
            $fieldMapping['originalField'] = $fieldMapping['fieldName'];
            $fieldMapping['fieldName'] = $property . "." . $fieldMapping['fieldName'];

            $fieldMapping['columnName'] = ! empty($this->embeddedClasses[$property]['columnPrefix'])
                    ? $this->embeddedClasses[$property]['columnPrefix'] . $fieldMapping['columnName']
                        : $this->namingStrategy->embeddedFieldToColumnName($property, $fieldMapping['columnName'], $this->reflClass->name, $embeddable->reflClass->name);

            $this->mapField($fieldMapping);
        }
    }

So it is ignored if it's empty because of

! empty($this->embeddedClasses[$property]['columnPrefix'])

So one possibility could be to change this so it accepts empty values or add a switch noPrefix = bool
Not sure if this would break in other places, though. Also it seems quite hacky.

I think a better solution would be to add something like associationKey, maybe embeddedKey, to the mapping.

Would be really great if you could add that feature. I think a Value Object to represent the id of an entity is a must have in a DDD application. It would be possible to passt that id object around, instead of a plain string/integer.

@doctrinebot

Comment created by antonstoeckl:

DDC-3028 was created from my pull request that fixes this issue

@doctrinebot

Issue was closed with resolution "Duplicate"

@doctrinebot

Comment created by pinkeen:

There is one problem with this. The auto generation strategy on the id field in the embeddable object is ignored.

SomethingId:
    type: embeddable
    id:
        id:
            type: integer
            generator: {strategy: AUTO}
@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.