简体   繁体   English

教义2:了解实体如何与教义2一起工作

[英]Doctrine 2: Understand how entities works with doctrine 2

I have 3 Entities: Person, Affiliation and PersonAffiliation. 我有3个实体:Person,Affiliation和PersonAffiliation。

In my form, I will display each affiliation as a checked checkbox. 在我的表单中,我会将每个从属关系显示为选中的复选框。

Nowm when the user uncecks the checkbox and click submit, this affiliation should be removed from the PersonAffiliation table. 现在,当用户取消选中复选框并单击“提交”时,应从PersonAffiliation表中删除此从属关系。

The problem is that when I submit without unchecking, my data are duplicated. 问题是,当我提交而不取消选中时,我的数据是重复的。 Example: aff1 and aff2. 示例:aff1和aff2。 When both checked and submit, I will then get aff1 aff1 aff2 aff2. 同时选中并提交后,我将得到aff1 aff1 aff2 aff2。 The same, If I uncheck aff2, I will then have aff1 aff1. 同样,如果我取消选中aff2,那么我将拥有aff1 aff1。

The error is probably somewhere in using the doctrine: 该错误可能是在使用该理论时出现的地方:

Here is how I am managing that: 这是我的管理方式:

  • Entity Persom 实体透视

     @ORM\\OneToMany(targetEntity="PersonAffiliation", mappedBy="person", cascade={"persist", "remove"}) protected $affiliations; 
  • Entity Affiliation: 实体隶属关系:

     @ORM\\OneToMany(targetEntity="PersonAffiliation", mappedBy="affiliation") protected $person_affiliations; 
  • Entity 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; 

An idea on how to resolve that? 有关如何解决该问题的想法?

Thank you. 谢谢。

EDIT: 编辑:

Cotroller part: 控制器部分:

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();
}

Form Part: 表单部分:

   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,
        ));
    }
}

In the Person entity : 在“个人”实体中:

/**
* @ORM\ManyToMany(targetEntity="Affiliation", inversedBy="people")
* @ORM\JoinTable(name="PersonAffiliation")
*/
protected $affiliations;

And in the Affiliation entity : 在关联实体中:

/**
* @ORM\ManyToMany(targetEntity="Person", mappedBy="affiliations")
*/
protected $people;

You made a $em->flush() on each iteration of the foreach. 您在foreach的每次迭代中都制作了$em->flush() It should be done after the end of foreach isnt'it ? 它应该在foreach结束后执行吗?

Moreover, you may use a ManyToMany relation. 此外,您可以使用ManyToMany关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM