[英]Symfony2 and Doctrine2 : populate form with two Entity (a complicated scenario)
我有5個實體:
這是架構:
一些細節:
WebUser是在網站上注冊的人員。 對於每個Webuser,都有一個人員ID。
一個人可以是網絡用戶,作者等。
每個WebUser都有0個或更多個從屬關系。 這些隸屬關系是由這個WebUser創建的,並在能干的UserAffiliations中鏈接。
Web用戶還可以將他創建的從屬關系鏈接到一個人(如果該人是作者),並且將填充實體人員關系。
我現在正試圖讓webuser為作者(人)分配一個聯盟。 為此,我有:
在實體人員中
@ORM\\OneToMany(targetEntity="PersonAffiliation", mappedBy="person", cascade={"persist", "remove"}) protected $affiliations;
在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;
在實體用戶中:
@ORM\\OneToMany(targetEntity="UserAffiliation", mappedBy="user") protected $affiliations; @ORM\\ManyToOne(targetEntity="Person") @ORM\\JoinColumn(name="person_id", referencedColumnName="id") protected $person;
在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;
在表單中,我正在做下一個:
$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,
));
但是這一切都沒有按照我的意願運作。
說明 :當我嘗試添加新的聯盟時,它僅為WebUser添加,我無法通過表單將其鏈接到作者(Person)。
你對如何解決這個問題有所了解,或者是一個好的教程嗎?
這應該在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
)
)
);
}
您可能還必須在關聯映射中設置級聯持久性。 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"]
從理論上講,symfony會將entity2置於引擎之下,使第二個if null子句變得不必要,但這總是困擾我,所以我更喜歡明確地做。
好吧,如果這個表單綁定到WebUser實體的集合是因為你傳遞給Controller中的表創建這樣的類的對象,它意味着:
$webUser = new WebUser();
$this->createForm(new SubmissionAffiliationFormType(), $webUser);
或者您通過不設置DefaultOptions並明確告知必須綁定到的data_class來委托決定使用哪個類到Symfony Forms:
class SubmissionAffiliationFormType extends AbstractType
{
//...
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\DemoBundle\Entity\Person',
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.