在 Symfony2 中嵌入集合类型

Embed a collection type in Symfony2

尝试嵌入一个集合类型,基本上按照逐步方法here

我收到以下错误:

The form's view data is expected to be an instance of class
AppBundle\Entity\BenefitGroup, but is an instance of class 
AppBundle\Entity\BenefitItem. You can avoid this error by setting the 
"data_class" option to null or by adding a view transformer that transforms 
an instance of class AppBundle\Entity\BenefitItem to an instance of 
AppBundle\Entity\BenefitGroup.

澄清一下,BenefitItem 是父项,而 BenefitGroup 是子项。

基本上我遇到 this 错误。

我还没有实现(还)允许您动态添加 BenefitGroup 元素的部分,我什至没有尝试保留对象或删除它(所以我还没有实现示例中解释的学说)。

这是我的代码:

BenefitItem 实体:

<?php
// src/AppBundle/Entity/BenefitItem.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\BenefitItemRepository")
 * @ORM\Table(name="benefit_items")
 */
class BenefitItem
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=400)
 */
protected $comment;

public function __construct()
{
    $this->BenefitGroups = new ArrayCollection();
}

public function getBenefitGroups()
{
    return $this->BenefitGroups;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set comment
 *
 * @param string $comment
 * @return BenefitItem
 */
public function setComment($comment)
{
    $this->comment = $comment;

    return $this;
}

/**
 * Get comment
 *
 * @return string 
 */
public function getComment()
{
    return $this->comment;
}
}

BenefitGroup 实体:

<?php
// src/AppBundle/Entity/BenefitGroup.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\BenefitGroupRepository")
 * @ORM\Table(name="benefit_groups")
 */
class BenefitGroup
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=100)
 */
protected $name;

/**
 * @ORM\ManyToOne(targetEntity="BenefitItem", cascade={"persist"})
 * @ORM\JoinColumn(name="benefitItem_id")
 */
protected $benefitItem;


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return BenefitGroup
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set benefitItem
 *
 * @param \AppBundle\Entity\BenefitItem $benefitItem
 * @return BenefitGroup
 */
public function setBenefitItem(\AppBundle\Entity\BenefitItem $benefitItem = null)
{
    $this->benefitItem = $benefitItem;

    return $this;
}

/**
 * Get benefitItem
 *
 * @return \AppBundle\Entity\BenefitItem 
 */
public function getBenefitItem()
{
    return $this->benefitItem;
}
}

BenefitItemFormType:

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BenefitItemFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('comment');

    $builder->add('benefitgroups', 'collection', array('type' => new BenefitGroupFormType()));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\BenefitGroup',
    ));
}

public function getName()
{
    return 'BenefitItem';
}
}

BenefitGroupFormType:

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BenefitGroupFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\BenefitGroup',
    ));
}

public function getName()
{
    return 'BenefitGroup';
}
}

控制器:

<?php
// AppBundle\Controller\BenefitController.php

namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\BenefitItem;
use AppBundle\Entity\BenefitGroup;
use AppBundle\Form\Type\BenefitItemFormType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class BenefitController extends Controller
{
/**
 * @Route("/benefit/show", name="benefit_show")
 */
public function showAction(Request $request)
{
    $BI = new BenefitItem();

    $BG1 = new BenefitGroup();
    $BG1->setName = 'Name 1';
    $BI->getBenefitGroups()->add($BG1);
    $BG2 = new BenefitGroup();
    $BG2->setName = 'Name 2';
    $BI->getBenefitGroups()->add($BG2);        

    $form = $this->createForm(new BenefitItemFormType(), $BI);

    $form->handleRequest($request);

    if ($form->isValid()) {
        // ... maybe do some form processing, like saving the Task and Tag objects
    }

    return $this->render('benefit/show.html.twig', array(
        'form' => $form->createView(),
    ));
}
}

有什么想法吗?

你说,你的data_classBenefitGroup

$resolver->setDefaults(array(
    'data_class' => 'AppBundle\Entity\BenefitGroup',
));

然后你设置一个BenefitItem.

$BI = new BenefitItem();
// ...
$form = $this->createForm(new BenefitItemFormType(), $BI);

那不行。我没有检查你的其余代码,但这也是异常所说的。