Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2987

Possibility to use a field / fields from an Embeddable as primary key(s)

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: Git Master
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      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.

        Activity

        Hide
        Anton Stöckl added a comment -

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

        Show
        Anton Stöckl added a comment - DDC-3028 was created from my pull request that fixes this issue

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Anton Stöckl
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: