I have 5 entities:
- User,
- Person,
- UserAffiliation,
- PersonAffiliation and
- Affiliation
Here is the schema:
Some details:
A WebUser is a Person who is registered to the website. For each Webuser, there is a person ID.
A person can be a web user, an author etc.
Each WebUser has 0 or more affiliations. Those affiliations were created by this WebUser and linked in the able UserAffiliations.
The WebUser can also link the affiliations he createed to a person (if the person is an author) and the Entity PersonAffiliation will be populated.
I am trying now to give the possibility to the webuser to assign an affiliation to an author (person). For that, the I have:
In the Entity Person
@ORM\OneToMany(targetEntity="PersonAffiliation", mappedBy="person", cascade={"persist", "remove"}) protected $affiliations;
In the PersonAffiliation
@ORM\ManyToOne(targetEntity="Person", inversedBy="affiliations") @ORM\JoinColumn(name="person_id", referencedColumnName="id") protected $person; @ORM\ManyToOne(targetEntity="Affiliation", inversedBy="person_affiliations") @ORM\JoinColumn(name="affiliation_id", referencedColumnName="id") protected $affiliation;
In the Entity User:
@ORM\OneToMany(targetEntity="UserAffiliation", mappedBy="user") protected $affiliations; @ORM\ManyToOne(targetEntity="Person") @ORM\JoinColumn(name="person_id", referencedColumnName="id") protected $person;
In the Entity UserAffiliation
@ORM\ManyToOne(targetEntity="User", inversedBy="affiliations") @ORM\JoinColumn(name="user_id", referencedColumnName="id") protected $user; @ORM\ManyToOne(targetEntity="Affiliation", inversedBy="user_affiliations") @ORM\JoinColumn(name="affiliation_id", referencedColumnName="id") protected $affiliation;
In the form, I am doing the next:
$builder->add('affiliations', 'entity', array(
'class' => 'SciForumVersion2Bundle:PersonAffiliation',
'query_builder' => function($em) use ($person){
return $em->createQueryBuilder('pa')->where('pa.person_id = :id')->setParameter('id', $person->getId());
},
'property' => 'affiliation',
'multiple' => true,
'expanded' => true,
));
But all this is not working properly as I would like.
Explanation: When I try to add a new affiliation, it is only added for the WebUser and I can't link it through the form to the author (Person).
Do you have an idea on how to resolve this, or maybe a good tutorial?