pierotto
pierotto

Reputation: 693

Doctrine YML mapping references

Resolving store entity "producer", which includes array with translations that belong to this entity.

Saving the database to succeed. However, in a table that contains translations missing link to the table of "producer".

This result:

my_producer:

+----+----+
| id |code|
+----+----+
| 1  |abcd|
+----+----+

my_producer_translations

+----+-----------+----+------+
| id |id_producer|name|locale|
+----+-----------+----+------+
| 1  |    NULL   |abcd|  en  |
+----+-----------+----+------+
| 2  |    NULL   |abcd|  de  |
+----+-----------+----+------+

Entity ProducerTranslation

ProducerTranslation
{
    protected $id;
    protected $name;
    protected $producer;
    protected $locale;

    ... getters and setters
}

Entity Producer

Producer
{
    protected $id;
    protected $code;
    protected $translations;

    public function __construct()
    {
        $this->translations = new ArrayCollection;
    }

    ... getters and setters, method for adding translations (entity ProductTranslation)
}

Producer YML

My\ProducerBundle\Entity\Producer:
  type: entity
  table: my_producer
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    code:
      type: string
      length: 50
      nullable: true

  oneToMany:
    translations:
      targetEntity: ProducerTranslation
      mappedBy: producer
      cascade: ["persist", "remove"]

ProducerTranslation YML

My\ProducerBundle\Entity\ProducerTranslation:
  type: entity
  table: my_producer_translations
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
      length: 100
    locale:
      type: text
      length: 2

  manyToOne:
    producer:
      targetEntity: Producer
      inversedBy: translations
      joinColumn:
        name: producer_id
        referencedColumnName: id
        onDelete: CASCADE

Upvotes: 3

Views: 82

Answers (1)

Max P.
Max P.

Reputation: 5679

Before save set Producer in ProducerTranslation $ProducerTranslation->setProducer($Producer)
Why did you allowed nulls for ProducerTranslation.id_producer?

Upvotes: 2

Related Questions