1

我有这两种形式:

编辑匹配实体的阵容字段的阵容表单

<?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';
    }
}

创建/编辑阵容实体的阵容表格

<?php

namespace Acme\MatchBundle\Form\Type;

use Acme\PlayerBundle\Entity\PlayerRepository;
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 LineupType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('player', 'entity', array(
                'class' => 'AcmePlayerBundle:Player',
                'property' => 'id',
                'query_builder' => function(PlayerRepository $er) {
                        $query = $er->createQueryBuilder('p');
                        return $query;
                    }
                ))
                ->add('status', 'text')
        ;
    }

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

    public function getName()
    {
        return 'match';
    }
}

阵容实体字段

/**
 * @var Match $match
 *
 * @ORM\ManyToOne(targetEntity="Match", inversedBy="lineups")
 * @Assert\NotBlank()
 */
private $match;

/**
 * @var \Acme\PlayerBundle\Entity\Player $player
 *
 * @ORM\ManyToOne(targetEntity="Acme\PlayerBundle\Entity\Player", inversedBy="lineups")
 * @Assert\NotBlank()
 */
private $player;

/**
 * @var string
 *
 * @ORM\Column(name="status", type="string", length=16)
 * @Assert\NotBlank()
 */
private $status;

现在我已经成功地将 Lineup 实体添加/删除到 lineups 字段,我想要将 lineup 实体的 $match 字段设置为使用 lineups 表单编辑的匹配。

那可能吗?

4

1 回答 1

4

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

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

哪个工作正常;)

于 2012-12-17T10:23:56.097 回答