0

I have 2 entities, User and License, and a bridge entity UserLicense. The associations between User and License is ManytoMany. I've defined the ORM annotations as follows, but I have a problem. When I save a user and a license in the db, I don't have the data referred to UserLicense upgraded.

User.php

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\OneToMany(targetEntity="UserLicense", mappedBy="user_id")
 */
private $id;

License.php

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\OneToMany(targetEntity="UserLicense", mappedBy="license_id")
 */
private $id;

UserLicense.php

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="id")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user_id;

/**
 * @ORM\ManyToOne(targetEntity="License", inversedBy="id")
 * @ORM\JoinColumn(name="license_id", referencedColumnName="id")
 */
private $license_id;

What's wrong?

ZugZwang
  • 418
  • 1
  • 5
  • 14

1 Answers1

1

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

Nicolas
  • 571
  • 6
  • 13
  • The relation is many to many. A user can have Many licences and one licence can be associated with many users. – ZugZwang Sep 12 '16 at 09:11
  • I change for manyToMany, but the point is : let Doctrine manage the relation, you have to think with an ORM not the database. – Nicolas Sep 12 '16 at 09:29
  • @Nicolas unless `UserLicence` has additional fields of course. – Jakub Matczak Sep 12 '16 at 09:48
  • 'UserLicence' has additional fields. Take a look at the best answer here on how to manage ManytoMany relations: http://stackoverflow.com/questions/3542243/doctrine2-best-way-to-handle-many-to-many-with-extra-columns-in-reference-table – ZugZwang Sep 12 '16 at 12:08
  • Ok, so, in your case the oneToMany need to be mapped by an entity not an ID, because Doctrine manage them. I edit my answer – Nicolas Sep 13 '16 at 09:10