Symfony1 嵌入形式的symfony多对多
我有一个问题与embbed形式从模型有多对多的关系。嵌入表单将正确保存模型,但不会保存多对多关系 例如: Schema.yml:Symfony1 嵌入形式的symfony多对多,symfony1,doctrine,many-to-many,relation,Symfony1,Doctrine,Many To Many,Relation,我有一个问题与embbed形式从模型有多对多的关系。嵌入表单将正确保存模型,但不会保存多对多关系 例如: Schema.yml: Mother: columns: name: type: string(80) Color: columns: name: type: string(80) Child: columns: mother_id: type: integer name: type: strin
Mother:
columns:
name:
type: string(80)
Color:
columns:
name:
type: string(80)
Child:
columns:
mother_id:
type: integer
name:
type: string(80)
relations:
Mother:
class: Mother
local: mother_id
foreign: id
type: one
onDelete: cascade
foreignType: one
foreignAlias: Children
FavoriteColors:
class: Color
refClass: ChildColor
local: child_id
foreign: color_id
onDelete: cascade
foreignAlias: Children
ChildColor:
columns:
child_id:
type: integer
color_id:
type: integer
然后我只修改MotherForm.class.php:
class MotherForm extends BaseMotherForm
{
public function configure()
{
$this->embedForm('child', new ChildForm($this->getObject()->getChildren()));
}
}
class ChildForm extends BaseChildForm
{
public function configure()
{
unset($this['mother_id']);
}
}
和ChildForm.class.php:
class MotherForm extends BaseMotherForm
{
public function configure()
{
$this->embedForm('child', new ChildForm($this->getObject()->getChildren()));
}
}
class ChildForm extends BaseChildForm
{
public function configure()
{
unset($this['mother_id']);
}
}
我用条令生成模块:
php symfony doctrine:generate-module frontend mother Mother
放置一些颜色数据:
Color:
Color_1:
name: blue
Color_2:
name: red
Color_3:
name: green
Color_4:
name: purple
当我调用/frontend_dev.php/mother/new时,我可以添加一个新的,母亲和孩子的名字会更新,但最喜欢的颜色永远不会保存
如果我使用phpmyadmin和/edit调用在颜色和子级之间添加关系。然后,正确的颜色出现在“多选”中,但我无法编辑它
是Symfony的bug还是我应该做些别的
更新:
如果我为模型子级生成模块。我可以编辑最喜欢的颜色,但表单不再嵌入…我遇到了与您相同的问题。都是关于这个问题的,很多人为此浪费了很多时间 在罚单中,有些人给出了解决方案。你可以试试,但我不知道为什么在我的情况下它们不起作用 我成功地完成了一项任务。这是一篇非常有用的文章,它解释了这个错误的原因和解决方法 基本上,这个错误是由于在保存嵌入表单时,symfony在嵌入对象中调用
save()
,而不是在嵌入表单中调用,并且对象中的save()
不会更新相关表
要解决此问题,您必须覆盖//lib/form/doctrine/BaseFormDoctrine.class.php
(或等效的推进,但在以下代码中替换推进的条令):
通过以下方式创建函数bindEmbeddedForms()
:
public function bindEmbeddedForms($embedded_forms, $values)
{
if($this->isValid())
{
foreach ($embedded_forms as $name => $form)
{
$form->isBound = true;
$form->values = $values[$name];
if ($form->embeddedForms)
{
$this->bindEmbeddedForms($form->embeddedForms, $values[$name]);
}
}
}
}
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
parent::bind($taintedValues, $taintedFiles);
$this->bindEmbeddedForms($this->embeddedForms, $this->getValues());
}
通过以下方式声明调用其父函数的bind()
函数:
public function bindEmbeddedForms($embedded_forms, $values)
{
if($this->isValid())
{
foreach ($embedded_forms as $name => $form)
{
$form->isBound = true;
$form->values = $values[$name];
if ($form->embeddedForms)
{
$this->bindEmbeddedForms($form->embeddedForms, $values[$name]);
}
}
}
}
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
parent::bind($taintedValues, $taintedFiles);
$this->bindEmbeddedForms($this->embeddedForms, $this->getValues());
}
并通过以下方式覆盖saveEmbeddedForms()
:
public function bindEmbeddedForms($embedded_forms, $values)
{
if($this->isValid())
{
foreach ($embedded_forms as $name => $form)
{
$form->isBound = true;
$form->values = $values[$name];
if ($form->embeddedForms)
{
$this->bindEmbeddedForms($form->embeddedForms, $values[$name]);
}
}
}
}
public function bind(array $taintedValues = null, array $taintedFiles = null)
{
parent::bind($taintedValues, $taintedFiles);
$this->bindEmbeddedForms($this->embeddedForms, $this->getValues());
}
public function saveEmbeddedForms($con = null, $forms = null)
{
if (is_null($con))
{
$con = $this->getConnection();
}
if (is_null($forms))
{
$forms = $this->embeddedForms;
}
foreach ($forms as $key => $form)
{
if ($form instanceof sfFormDoctrine)
{
if(method_exists(new $form(), 'doSaveManyToMany'))
{
$form->doSaveManyToMany($con);
}
else
{
$form->getObject()->save($con);
}
$form->saveEmbeddedForms($con);
}
else
{
$this->saveEmbeddedForms($con, $form->getEmbeddedForms());
}
}
}
然后,在您的嵌入式表单类中(在文章中,它在baseformdoctor
类中也可以这样做,但我认为这更干净),创建方法dosavemanytomy
,它保存您的关系:
public function doSaveManyToMany($con = null)
{
if (is_null($con))
{
$con = $this->getConnection();
}
$this->object->save($con);
/*
* Save the many-2-many relationship
*/
$this->save***List($con); //Ex: $this->saveAhasBList($con)
}
有些人说在帖子评论中有一些问题并提出了解决方案,但这不是我的情况
希望它能帮助一些人,即使我的回答是针对一个很老的开放式问题。谢谢,你的评论非常有用。我不是一个更新symfony核心的球迷,我希望修复即将到来。。。我再也找不到它了,但我想我解决这个问题的方法是将它保存在父窗体中(我的示例中是母亲窗体)。