我有 3 个实体:Person、Affiliation 和 PersonAffiliation。
在我的表格中,我会将每个从属关系显示为选中的复选框。
现在,当用户取消选中复选框并单击提交时,应该从 PersonAffiliation 表中删除此从属关系。
问题是,当我在没有取消选中的情况下提交时,我的数据是重复的。示例:aff1 和 aff2。当检查并提交时,我会得到 aff1 aff1 aff2 aff2。同样,如果我取消选中 aff2,我将拥有 aff1 aff1。
错误可能在使用该学说的某个地方:
以下是我的管理方式:
实体 Persom
@ORM\OneToMany(targetEntity="PersonAffiliation", mappedBy="person", cascade={"persist", "remove"}) protected $affiliations;
实体隶属关系:
@ORM\OneToMany(targetEntity="PersonAffiliation", mappedBy="affiliation") protected $person_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;
关于如何解决这个问题的想法?
谢谢你。
编辑:
控制器部分:
foreach( $enquiry->getAffiliations() as $aff )
{
$pAff = new PersonAffiliation();
$pAff->setPersonId( $person->getId() );
$pAff->setAffiliationId( $aff->getAffiliation()->getId() );
$pAff->setPerson( $person );
$pAff->setAffiliation( $aff->getAffiliation() );
$em->persist($pAff);
$em->flush();
}
表格部分:
public function buildForm(FormBuilder $builder, array $options)
{
$person = $this->person;
$user = $this->user;
$builder->add('firstname', 'text');
$builder->add('middlename', 'text', array('required'=>false));
$builder->add('lastname', 'text');
$builder->add('sex', 'choice', array( 'choices' => array('m' => 'Male', 'f' => 'Female'),
'required' => true,
'multiple' => false,
'expanded' => true));
$builder->add('email', 'text', array('required'=>false));
if( $this->addAffiliations ) {
$builder->add('affiliations', 'entity', array(
'label' => 'Athor\'s affiliations',
'class' => 'SciForumVersion2Bundle:PersonAffiliation',
'query_builder' => function($em) use ($person, $user){
return $em->createQueryBuilder('pa')
->where('pa.person_id = :pid')
->setParameter('pid', $person->getId());
},
'property' => 'affiliation',
'multiple' => true,
'expanded' => true,
));
}
}