简体   繁体   English

在Symfony2中嵌入集合类型

[英]Embed a collection type in Symfony2

Trying to embed a collection type, basically following the step by step approach here . 尝试嵌入集合类型,基本上遵循此处的逐步方法。

I get the following error: 我收到以下错误:

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.

Just to clarify, the BenefitItem is the father while the BenefitGroup is the child. 需要说明的是,BenefitItem是父亲,BenefitGroup是孩子。

I'm getting this error, basically. 基本上,我收到错误。

I've not implemented (yet) the part that allows you to add BenefitGroup elements dynamically, and I'm not even trying to persist the object or remove it (so I did not implement yet the last part of the Doctrine as explained in the example). 我尚未实现(但尚未实现)允许您动态添加BenefitGroup元素的部分,甚至没有尝试保留该对象或将其删除(因此,我尚未实现该学说的最后一部分,如例)。

Here is my code: 这是我的代码:

The BenefitItem entity: 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;
}
}

The BenefitGroup entity: 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;
}
}

The BenefitItemFormType: 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';
}
}

The BenefitGroupFormType: 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';
}
}

The controller: 控制器:

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

Any idea? 任何想法?

You say, your data_class is a BenefitGroup 您说,您的data_classBenefitGroup

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

Then you set a BenefitItem . 然后设置一个BenefitItem

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

That cannot work. 那行不通。 I didn't check the rest of your code, but that's also what the exception says. 我没有检查其余的代码,但这也是异常所说明的。

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

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