在不使用javascript的情况下在Symfony中显示空表单集合项

时间:2013-02-27 05:44:10

标签: symfony symfony-2.1 symfony-forms

我正在尝试显示带有集合的表单。该集合应显示一个空子表单。由于项目性质,我不能依赖JavaScript这样做。

谷歌搜索没有帮助,我似乎没有通过向集合字段添加一个空实体来工作。

到目前为止我所拥有的:

public function indexAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $event = $em->getRepository('EventBundle:EventDynamicForm')->find($id);

    $entity = new Booking();
    $entity->addParticipant( new Participant() );
    $form   = $this->createForm(new BookingType(), $entity);

    return array(
        'event' => $event,
        'edit_form' => $form->createView()
    );
}

在BookingType.php buildForm()

$builder
    ->add('Participants', 'collection')

在Twig模板中

{{ form_row(edit_form.Participants.0.companyName) }}

如果我把行$ entity-> addParticipant(new Participant());在indexAction()中我得到一个错误说:

  

表单的视图数据应该是标量,数组或类型   \ ArrayAccess的实例,但是是类的实例   Yanic \ EventBundle \实体\参与者。你可以避免这个错误   将“data_class”选项设置为   “Yanic \ EventBundle \ Entity \ Participant”或通过添加视图转换器   转换类的实例   Yanic \ EventBundle \ Entity \参与标量,数组或实例   \ ArrayAccess。

如果我删除了所说的行Twig抱怨:

  

对象“Symfony \ Component \ Form \ FormView”的方法“0”不存在   /Applications/MAMP/htdocs/symfony-standard-2.1/src/Yanic/EventBundle/Resources/views/Booking/index.html.twig   在第27行

编辑:addParticipant是doctrine生成的默认方法:generate:entities命令

/**
 * Add Participants
 *
 * @param \Yanic\EventBundle\Entity\Participant $participants
 * @return Booking
 */
 public function addParticipant(\Yanic\EventBundle\Entity\Participant $participants)
 {
     $this->Participants[] = $participants;

     return $this;
 }

我确信我做错了什么,却找不到线索: - (

2 个答案:

答案 0 :(得分:7)

我猜你在Symfony2表单集上有点迷失,虽然我认为你已经阅读了http://symfony.com/doc/current/cookbook/form/form_collections.html。 在这里,我将强调文档,帮助其他SO读者,并在回答问题时练习自己.. :)

首先,您必须至少拥有两个实体。在您的情况下,BookingParticipant。在预订实体中,添加以下内容。由于您使用的是Doctrine,因此Participant必须包含ArrayCollection

use Doctrine\Common\Collections\ArrayCollection;

class Booking() {

    // ...

    protected $participants;

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

    public function getParticipants()
    {
        return $this->participants;
    }

    public function setParticipants(ArrayCollection $participants)
    {
        $this->participants = $participants;
    }
}

其次,您的参与者实体可以是任何东西。仅举例:

class Participant
{
    private $name;

    public function setName($name) {
        $this->name = $name;

        return $this;
    }

    public function getName() {
        return $this->name;
    }
}

第三,您的BookingType应包含ParticipantType的集合,如下所示:

// ...
$builder->add('participants', 'collection', array('type' => new ParticipantType()));

第四,ParticipantType很简单。根据我之前的例子:

// ...
$builder->add('name', 'text', array('required' => true));

最后,在BookingController中,添加必要数量的参与者以创建集合。

// ...
$entity = new Booking();

$participant1 = new Participant();
$participant1->name = 'participant1';
$entity->getParticipants()->add($participant1); // add entry to ArrayCollection

$participant2 = new Participant();
$participant2->name = 'participant2';
$entity->getParticipants()->add($participant2); // add entry to ArrayCollection

答案 1 :(得分:0)

认为你必须在这里添加类型:

 ->add('Participants', 'collection', array('type' => 'YourParticipantType'));

您是否还可以在此处粘贴模型中addParticipant函数的声明?似乎还有一些可疑的东西。