Here, there’s an explanation on “How to generate Entities from an Existing Database”.
I have a table person. And a table address. A person can have many different addresses, and an address can be related to one ore more persons. So this is a “many to many” relationship. Thus I’ve created an “in-between” table I’ve called personaddress where there’s a idPersonne and idAddress.
When launching the generation everything is fine but the many to many relationship (there are more than one).
PersonneAdresse:
type: entity
table: personne_adresse
fields:
id:
id: true
type: bigint
nullable: false
generator:
strategy: IDENTITY
manyToOne:
idPersonne:
targetEntity: Personne
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
id_personne:
referencedColumnName: id
orphanRemoval: false
idAdresse:
targetEntity: Adresse
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
id_adresse:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
The documentation about oneToMany is not enough (I need an example to understand):
If you have oneToMany relationships between your entities, you will
need to edit the generated xml or yml files to add a section on the
specific entities foroneToManydefining theinversedByand the
mappedBypieces.
And I’m pretty sure the modification is not only about oneToMany, but manyToMany too.
Can you explain me what I should do/modify to be able to generate properly the corresponding entities?
Here’s how I did the stuff: I’ve generated the whole files, but I knew there were some ManyToMany relationships. So I declared them “manually” in the corresponding files. Then I’ve encountered another problem. In the documentation, the codes samples aren’t 100% correct. You have to do this add “
@ORM\” instead of the simple “@“. Here my code that works:Moreover, you have to delete corresponding ManyToMany Php files, because Symfony 2 (I guess) handle those “in-between” ManyToMany tables itself => no need to declare. In my sample, I had to delete “
PersonneAdresse.php“, “PersonnePartenaire.php” and “PersonneTelephone.php“.