Les relations OneToOne
Prenons l’exemple d’une
table Reference liée à notre table produit.
Nous allons créer la table Reference
qui contiendra un seul champ : le numéro.
php bin/console make:entity
Il faut répondre aux questions comme
ci-dessous :
Class name of the entity to create or update (e.g. FierceElephant):
> Reference
created: src/Entity/Reference.php
created: src/Repository/ReferenceRepository.php
Entity generated! Now let’s add some fields!
You can always add more fields later manually or by re-running this
command.
New property name (press <return> to stop adding fields):
> numero
Field type (enter ? to see all types) [string]:
> int
Can this field be null in the database (nullable) (yes/no) [no]:
> no
Add another property? Enter the property name (or press <return> to stop
adding fields):
>
Success!
Créons la migration :
php bin/console make:migration
et mettons à jour la base :
php bin/console doctrine:migrations:migrate
La table Reference est maintenant créée.
Nous allons créer une jointure OneToOne entre l’entité Produit
et l’entité Reference.
Tout se passe dans l’entité propriétaire,
qui est ici l’entité Produit.
La relation OneToOne va se faire en ajoutant
une nouvelle propriété qui fera la jointure. Appelons
cette propriété ...