简体   繁体   English

Symfony2和Doctrine2:使用两个实体填充表单(一个复杂的场景)

[英]Symfony2 and Doctrine2 : populate form with two Entity (a complicated scenario)

I have 5 entities: 我有5个实体:

  • User, 用户,
  • Person, 人,
  • UserAffiliation, UserAffiliation,
  • PersonAffiliation and PersonAffiliation和
  • Affiliation 联系

Here is the schema: 这是架构:

在此输入图像描述

Some details: 一些细节:

  • A WebUser is a Person who is registered to the website. WebUser是在网站上注册的人员。 For each Webuser, there is a person ID. 对于每个Webuser,都有一个人员ID。

  • A person can be a web user, an author etc. 一个人可以是网络用户,作者等。

  • Each WebUser has 0 or more affiliations. 每个WebUser都有0个或更多个从属关系。 Those affiliations were created by this WebUser and linked in the able UserAffiliations. 这些隶属关系是由这个WebUser创建的,并在能干的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. Web用户还可以将他创建的从属关系链接到一个人(如果该人是作者),并且将填充实体人员关系。

I am trying now to give the possibility to the webuser to assign an affiliation to an author (person). 我现在正试图让webuser为作者(人)分配一个联盟。 For that, the I have: 为此,我有:

  • In the Entity Person 在实体人员中

     @ORM\\OneToMany(targetEntity="PersonAffiliation", mappedBy="person", cascade={"persist", "remove"}) protected $affiliations; 
  • In the PersonAffiliation 在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 在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). 说明 :当我尝试添加新的联盟时,它仅为WebUser添加,我无法通过表单将其链接到作者(Person)。

Do you have an idea on how to resolve this, or maybe a good tutorial? 你对如何解决这个问题有所了解,或者是一个好的教程吗?

This should be handled in the Entity1Controller.php: 这应该在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: 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. 从理论上讲,symfony会将entity2置于引擎之下,使第二个if null子句变得不必要,但这总是困扰我,所以我更喜欢明确地做。

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实体的集合是因为​​你传递给Controller中的表创建这样的类的对象,它意味着:

$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: 或者您通过不设置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.

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