Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 847
pierotto Avatar asked Dec 06 '25 05:12

pierotto


1 Answers

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

like image 71
Max P. Avatar answered Dec 08 '25 19:12

Max P.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!