为了保存 Media 对象,需要一个 id,因此可以在保存“父”对象后进行保存。我认为您拥有的最佳选择是saveEmbeddedFroms()
方法(以 MediaForm 嵌入的形式):
public function saveEmbeddedForms($con = null, $forms = null)
{
if (isset($this->embeddedForms['media']))
{
$mediaForm = $this->getEmbeddedForm('media');
if ($mediaForm instanceof MediaForm && $mediaForm instanceof sfFormDoctrine)
{
$mediaFrom
->getObject()
->set('entity', get_class($this->getObject()))
->set('entity_id', $this->getObject()->get('id'))
;
}
}
parent::saveEmbeddedForms($con, $forms);
}
实际上你也可以把它放到BaseFormDoctrine
类中,这样每一个嵌入了 MediaForm 的学说形式都会被正确保存。
编辑:最后sfFormObject对我有用
public function saveEmbeddedForms($con = null, $forms = null)
{
if (null === $con)
{
$con = $this->getConnection();
}
if (null === $forms)
{
$forms = $this->embeddedForms;
}
foreach ($forms as $form)
{
if ($form instanceof sfFormObject)
{
$form->getObject()->set('entity', get_class($this->getObject()))->set('entity_id', $this->getObject()->get('id'));
}
}
return parent::saveEmbeddedForms($con, $forms);
}