我有这两种形式:
用于编辑匹配实体的阵容字段的阵容
<?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;
现在我已成功添加/删除阵容实体到阵容字段,我想要的是将阵容实体的$ match字段设置为用阵容表格编辑的匹配。
这可能吗?
答案 0 :(得分: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);
}
);
哪种方法正常;)