尝试嵌入一个集合类型,基本上遵循一步一步的方法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(),
));
}
}
有什么想法吗?
答案 0 :(得分:0)
您说,您的data_class
是BenefitGroup
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\BenefitGroup',
));
然后设置BenefitItem
。
$BI = new BenefitItem();
// ...
$form = $this->createForm(new BenefitItemFormType(), $BI);
那不行。我没有检查你的其余代码,但这也是异常所说的。