Symfony 2嵌入集合和实体FieldTypes以保持连接表(1:M M:1 1:M)

时间:2015-02-27 08:17:24

标签: php symfony orm doctrine

我在一个场景中挣扎,可能会错过最后一块。

  

有很多能力

     

能力具有年份属性和商业经验(可选)

     

能力类型(例如:每日/每月等等

     

- 1:M - 能力 M:1人/ M:1种类型 - 类型 1:M能力< / p>

人/技能有级联持续/删除以及孤立删除设置。

用户互动

用户创建他们的初始个人资料,他们会进入能力页面并点击与他们的能力相关的复选框,相应的(可选)复选框以获得他们的商业体验,以及多年经验的下拉菜单。

意图:

当用户选择他们的能力并提交时 - 相应的能力与用户(以及任何属性)相关联。然后保持不变,忽略未检查其能力标题的任何行(禁用等)。

ABILITIES表格:已更新

$builder
->add('abilitytitle', 'collection', array(
        'by_reference' => false,
        'type' => new AbilityTitleType(),
        'data_class'   => 'AppBundle\Entity\AbilityTitle',
    ))
->add('commercialexperience','collection',array(
    'type' => 'checkbox', 
    'allow_add' => true,
    'options' => array(
        'label' => 'Commercial Experience',
        'required'  => false,)
))
->add('experience','collection',array(
    'type' => 'choice',
    'allow_add' => true,
    'options' => array(
        'placeholder' => 'Please select',
        'choices' => 
            array(
             '1-2' => '1-2',
             '3-4' => '3-4',
            ),
    'required'  => false, 
    )
))

对于表单,我手动打印的经验和商业经验与能力信息一致。我创建了一个AbilityTitleType并将其作为集合插入,但它似乎让我偏离了轨道。

CONTROLLER

    public function newUserAction(Request $request)
{
    $person= new Person();

    $form = $this->createForm(new PersonEntry(),$person);
    $form->handleRequest($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($person);
        $em->flush();
        return $this->redirect($this->generateUrl('qualifications'));
    }

    return $this->render('AppBile:User:newPerson.html.twig', array(
        'form' => $form->createView(),
    ));
}

这是一个通用的表单流 AbilityTitleType

    public function buildForm(FormBuilderInterface $builder, array $options)
{ 
    $builder
        ->add('abilityname', 'entity', array(
                'class' => 'AppBundle:AbilityTitle',
                'property' => 'abilityname',
                'multiple' => true, 
                'expanded' => true, 
                'required'  => false, 
                'label' => 'Please select claims in which you have experience'
            ));
}
public function getName()
{
    return 'abilitytitle';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBile\Entity\AbilityTitle'
    ));
}

问题:功能标题在嵌入时在表单中显示为空白

我的吸气剂和制定者是协会的典型代表,可以根据需要提供,用户和能力的捷径。

问题

我遇到了错误&#34;表格的视图数据应该是一个类的实例...给出为ArrayCollection&#34;我的能力冠军。我想这是由于多线插入,因为下面的虚拟代码插入很好。我相信我的问题是关于集合,嵌入和实体字段类型。

我在看一个场景,其中每一行本身就是一个实体,我需要抓住表格收到的数据,分别保留每一行?

嵌入式实体表单的集合,其中行可能因行数可能包含的行数而异?如果是这样的话,我还没有完全想象它!

感谢您的任何指示和寻找!很高兴在必要时上传更多代码。

// DUMMY CODE - Inserts Fine  
for($i = 0; $i < 5; ++$i) {
    $person= $em->find('person', 1);
    $person_abilities->setExperience(rand(1,10))
       ->setCommercialExperience(rand(1,10));
    $ability= $this->getDoctrine()
       ->getRepository('AppBundle:AbilityTitle')
       ->findOneById(rand(1,3));
}
$potential->addPerson_Abilities($person_abilities);
$ability->addPerson_Abilities($person_abilities);

1 个答案:

答案 0 :(得分:0)

那是因为您必须设置data_class选项。

例如:

->add('test', 'collection', array(
        'type'               => new TestFormType(),
        'options'            => array(
            'data_class'   => 'TestBundle\Entity\Test',
        ),
    )
);
编辑:忘了其他的东西。您可能有兴趣阅读this(向下滚动到Doctrine: Cascading Relations and saving the "Inverse" side)。