Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 2设置集合字段实体的默认值_Symfony_Symfony 2.1_Symfony Forms - Fatal编程技术网

Symfony 2设置集合字段实体的默认值

Symfony 2设置集合字段实体的默认值,symfony,symfony-2.1,symfony-forms,Symfony,Symfony 2.1,Symfony Forms,我有以下两种表格: 用于编辑匹配实体的“列队”字段的列队表单 <?php namespace Acme\MatchBundle\Form\Type; use Acme\UserBundle\Entity\UserRepository; use Acme\TeamBundle\Entity\TeamRepository; use Acme\ApiBundle\Listener\PatchSubscriber; use Acme\CoreBundle\Form\DataTransforme

我有以下两种表格:

用于编辑匹配实体的“列队”字段的列队表单

<?php

namespace Acme\MatchBundle\Form\Type;

use Acme\UserBundle\Entity\UserRepository;
use Acme\TeamBundle\Entity\TeamRepository;
use Acme\ApiBundle\Listener\PatchSubscriber;
use Acme\CoreBundle\Form\DataTransformer\TimestampToDateTimeTransformer;

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

class LineupsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('lineups', 'collection', array(
                'type'         => new LineupType(),
                'allow_add'    => true,
                'allow_delete' => false
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'        => 'Acme\MatchBundle\Entity\Match',
            'csrf_protection'   => false
        ));
    }

    public function getName()
    {
        return 'match';
    }
}
现在,我已经成功地将列队实体添加/删除到“列队”字段,我想要的是将列队实体的$match字段设置为使用列队表单编辑的匹配


这可能吗?

我自己发现绑定到绑定表单事件可以让我做我需要的事情:

$builder->addEventListener(
    FormEvents::BIND,
    function (FormEvent $event) {
        $data = $event->getData();
        $lineups = $data->getLineups();
        foreach ($lineups as &$lineup) {
            $lineup->setMatch($data);
        }
        $event->setData($data);
    }
);

这很好;)

我自己发现绑定到BIND form事件可以让我做我需要的事情:

$builder->addEventListener(
    FormEvents::BIND,
    function (FormEvent $event) {
        $data = $event->getData();
        $lineups = $data->getLineups();
        foreach ($lineups as &$lineup) {
            $lineup->setMatch($data);
        }
        $event->setData($data);
    }
);

这很好;)

列队数组以1开头,因为它是数据库中第一个列队的ID。问题是我不需要执行删除/添加操作,因为我可以使用ArrayCollection作为参考,只需编辑它。列队数组以1开头,因为这是数据库中第一个列队的ID。问题是我不需要执行删除/添加操作删除/添加内容,因为我可以使用ArrayCollection作为参考,只需编辑它。
$builder->addEventListener(
    FormEvents::BIND,
    function (FormEvent $event) {
        $data = $event->getData();
        $lineups = $data->getLineups();
        foreach ($lineups as &$lineup) {
            $lineup->setMatch($data);
        }
        $event->setData($data);
    }
);