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

Mic*_*chi 4 symfony-forms symfony symfony-2.1

我正在尝试显示带有集合的表单.该集合应显示一个空子表单.由于项目性质,我不能依赖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()
    );
}
Run Code Online (Sandbox Code Playgroud)

在BookingType.php buildForm()中

$builder
    ->add('Participants', 'collection')
Run Code Online (Sandbox Code Playgroud)

在Twig模板中

{{ form_row(edit_form.Participants.0.companyName) }}
Run Code Online (Sandbox Code Playgroud)

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

表单的视图数据应该是标量,数组或\ ArrayAccess的实例,但是是Yanic\EventBundle\Entity\Participant类的实例.您可以通过将"data_class"选项设置为"Yanic\EventBundle\Entity\Participant"或添加视图转换器来将此类错误设置为"Yanic\EventBundle\Entity\Participant"转换为标量,数组或实例\ 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;
 }
Run Code Online (Sandbox Code Playgroud)

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

ihs*_*san 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

class Participant
{
    private $name;

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

        return $this;
    }

    public function getName() {
        return $this->name;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

// ...
$builder->add('participants', 'collection', array('type' => new ParticipantType()));
Run Code Online (Sandbox Code Playgroud)

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

// ...
$builder->add('name', 'text', array('required' => true));
Run Code Online (Sandbox Code Playgroud)

最后,在BookingController中,添加必要数量的Participant以创建集合.

// ...
$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
Run Code Online (Sandbox Code Playgroud)