See comments, relation is ManyToMany, with extra fields
You have to do the relations between entities, and doctrine manage them.
You say to doctrine, I want a relation between users and userLicences. Doctrine manage the join column with the right fields, defaults values are good most of the time, this annotation can be delete:
@ORM\JoinColumn(name="user_id", referencedColumnName="id")
Try this :
User.php
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/*
*@ORM\OneToMany(targetEntity="UserLicense", mappedBy="user")
*/
private $userLicence;
License.php
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/*
* @ORM\OneToMany(targetEntity="UserLicense", mappedBy="license")
*/
private $userLicence;
UserLicense.php
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="userLicence")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="License", inversedBy="userLicence")
* @ORM\JoinColumn(name="license_id", referencedColumnName="id")
*/
private $license;
-- First answer before comments ---
If I understand correctly what you want to do, you just have to setup a relation between User and Licence.
Doctrine take care of the needed database table to handle the relation.
You don't need to worry about that.
User can have many Licence, and Licence can be owned by many users, you can do this :
User.php
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
private $id;
/*
* @ORM\ManyToMany(targetEntity="License", mappedBy="user")
*/
private $licence;
Lincence.php
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/*
* @ORM\ManyToMany(targetEntity="User", ReversedBy="license")
*
*/
private $user;
Private $user;
You have to add the setter and getter of $licence and $user, and when you add a Licence to a User, Doctrine will do the association.
Something like
$licence = new Licence();
$user->addLicence($licence);
After, you need to flush();
It's just some example code, how you implement it depends on your work.
Some good resource:
https://knpuniversity.com/screencast/symfony2-ep3/many-to-many-relationship
This one is perfect to understand manyToMany relation
Must read :
http://doctrine-orm.readthedocs.io/en/latest/reference/association-mapping.html
You have to see the doctrine capability fetch="EAGER" , if you want to have the associated entity in some case, and read about ArrayCollection