I have 5 entities:
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?
This should be handled in the Entity1Controller.php:
public function createAction(Request $request)
{
$securityContext = $this->get('security.context');
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(new Entity1Type()
,null,array('attr' => array('securitycontext' => $securityContext)));
$form->bind($request);
if ($form->isValid()){
$data = $form->getData();
$entity1id = $data->getId();
$entity2id = $data->getEntity2Id();
$entity1medicaid=$data->getMedicaidID();
$entity1=$em->getRepostiory('projectBundle:Entity1')->findOneById($entity1id);
$entity2=$em->getRepository('projectprojectBundle:Entity2')->findOneById($entity2id);
if (null === $entity1){
$entity1=new entity1();
$entity1->setEntity2id($entity2id);
$entity1->setID($entity1id);
}
if (null === $entity2){
$entity2=new entity2();
$entity2->setID($entity2id);
}
$em->persist($entity1);
$em->persist($entity2);
$em->flush();
return $this->redirect($this->generateUrl('entity1', array()));
}
return $this->render('Bundle:entity1:new.html.twig', array(
'form' => $form->createView()
,'attr' => array('securitycontext' => $securityContext
)
)
);
}
You may also have to set cascade persist in your association mappings. Entity1.yml:
project\projectBundle\Entity\Entity1:
type: entity
table: entity1
repositoryClass: project\projectBundle\Entity\Entity1Repository
fields:
id:
type: bigint
id: true
generator:
strategy: AUTO
property:
type: string
length: 255
unique: true
manyToMany:
entity2:
targetEntity: entity2
mappedBy: entity1
cascade: ["persist"]
In theory, symfony will make entity2 under the hood, making the second if null clause unnecessary, but that always bothers me, so I prefer to do it explicitly.
Well if this form is binding the collection to a WebUser Entity is because you are passing in to the Form creation in the Controller an object of such class, it means:
$webUser = new WebUser();
$this->createForm(new SubmissionAffiliationFormType(), $webUser);
or you are delegating the decision of which class to use to Symfony Forms by not setting the DefaultOptions and telling explicitly the data_class it must be bind to:
class SubmissionAffiliationFormType extends AbstractType
{
//...
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\DemoBundle\Entity\Person',
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With