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

doctrinebot opened this Issue Feb 19, 2014 · 3 comments

2 participants


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:

    type: entity
    table: car
    repositoryClass: *my_namespace*\ConcreteCarRepository
            type: string
            length: 100
            class: CarUuidValue
            class: CarModelValue

    type: embeddable
            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);


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.


Comment created by antonstoeckl:

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


Issue was closed with resolution "Duplicate"


Comment created by pinkeen:

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

    type: embeddable
            type: integer
            generator: {strategy: AUTO}
@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment