这是我在这里的第一个问题,所以请原谅任何错误 - 下次我会尽量避免它们。;-)
我为 FOSUserBundle 编写了一个自定义 RegistrationFormType。除了包的默认字段之外,此表单还处理 PlayerType。这个 PlayerType 本身又包含一个 PlayerSkillsType。这里的类:
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilder $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add('player', new PlayerType());
}
public function getName()
{
return 'signup_form';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\AcmeBundle\Entity\User',
);
}
}
class PlayerType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('firstname');
$builder->add('lastname');
$builder->add('age');
$builder->add('playerSkills', new PlayerSkillsType());
}
public function getName()
{
return 'player_form';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\AcmeBundle\Entity\Player',
);
}
}
class PlayerSkillsType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('tackling');
$builder->add('passing');
$builder->add('shooting');
}
public function getName()
{
return 'playerSkills_form';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\AcmeBundle\Entity\PlayerSkills',
);
}
}
/**
* @ORM\Entity
*/
class Player
{
/**
* @ORM\OneToOne(targetEntity="PlayerSkills", cascade={"persist"})
*
* @var PlayerSkills
*/
private $playerSkills;
}
/**
* @ORM\Entity
*/
class PlayerSkills
{
/**
* @ORM\OneToOne(targetEntity="Player", cascade={"persist"})
*
* @var Player
*/
private $player;
}
(我省略了 getter 和 setter 以及不重要的属性和方法。)到目前为止,这工作正常,表单已显示并保持不变。现在,我的问题是,在持久化数据后,数据中的 PlayerSkills 实体缺少对 Player 实体的引用。
我认为我需要告诉 PlayerSkillsType 它还应该在表单构建器中添加引用..?或者这可能是 Doctrine 注释中的问题?
任何提示都非常感谢!:-)