早上好
我在这里向社区征求关于嵌入式表格与Symfony 1.4 的信息
我有两个模型,关系是一对多
我在显示表单之前生成第一个模型及其所有依赖项
我总是得到那些错误:
modelb: Unexpected extra form field named "0". Unexpected extra form field named "1". Unexpected extra form field named "2". Unexpected extra form field named "3". Unexpected extra form field named "4". Unexpected extra form field named "5". Unexpected extra form field named "6". Unexpected extra form field named "7". Unexpected extra form field named "8". Unexpected extra form field named "9". Unexpected extra form field named "10". Unexpected extra form field named "11".
我也尝试了embedRelation()
,我也得到了同样的结果。
以下是两个模型模式的摘要:
ModelA:
columns:
model_d_id: { type: integer, notnull: true }
comments: { type: string(1024), notnull: false }
relations:
ModeleD: ...
ModelB:
columns:
model_a_id: { type: integer, notnull: true }
model_b_id: { type: integer, notnull: false }
model_c_id: { type: integer, notnull: true }
is_selected: { type: boolean, notnull: true }
relations:
ModelA:
class: ModelA
local: model_a_id
foreign: id
onDelete: CASCADE
foreignAlias: ModelB
ModelB: ...
ModelC: ...
因此,在表单中,我希望能够更新comments
的{{1}},并为与ModelA
相关的is_selected
的每个条目设置ModelB
的复选框目前的ModelA。
这是表格的样子:
[] modele_b_entry_name_1
[] modele_b_entry_name_2
[] modele_b_entry_name_3
text
[_______________________________]
[ Submit ]
以下是嵌入表单的代码:
class ModelBForm extends BaseModelBForm
{
public function configure()
{
unset($this['model_a_id'], $this['model_b_id'], $this['model_c_id'], $this['updated_at']);
$model_a = $this->getObject();
$this->setWidget('is_selected', new sfWidgetFormInputCheckbox(
array('label' => $model_a->ModelC->getName()),
array()
));
$this->setValidator('is_selected', new sfValidatorBoolean());
}
}
以下是嵌入式表单的集合:
class ModelBCollectionForm extends sfForm
{
public function configure()
{
$all_model_b = Doctrine_Query::create()
->from('ModelB p')
->where('p.model_a_id = ?', $this->getOption('model_a_id'))
->execute();
foreach ($all_model_b as $index => $model_b) {
$form = new ModelBForm($model_b);
$this->embedForm($index, $form);
}
}
}
现在全球形式:
class ModelAForm extends BaseModelAForm
{
public function configure()
{
unset($this['created_at'], $this['model_d_id']);
$subForm = new ModelBCollectionForm(null,
array('model_a_id' => $this->getObject()->getId())
);
$this->embedForm('allmodelb', $subForm);
}
}
模板只是简单地显示表格 因此,第一个操作显示表单,包含表的现有条目,然后在提交表单时,以下是代码:
public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
$this->form = new ModelAForm();
var_dump($request->getParameter($this->form->getName()));
$this->form->bind($request->getParameter($this->form->getName()));
foreach($this->form->getErrorSchema()->getErrors() as $key => $error)
{
echo '<p>' . $key . ': ' . $error . '</p>';
}
var_dump($this->form->getValues());
}
第一个var_dump()
显示一个包含良好数据的完整数组。第二个是空的
我正在努力工作几个小时,但我没有成功将表单与POST数据绑定。
感谢您的帮助!
编辑:我试图覆盖绑定方法,但它不起作用...除非我取消设置ModelB数据,但save()
方法将执行INSERT而不是UPDATE。
答案 0 :(得分:0)
我解决了我的问题 这是解决方案:
public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
$model_a_array = $request->getParameter('model_a');
$id_model_a = $model_a_array['id'];
$model_a = Doctrine::getTable('ModelA')->find($id_model_a);
$this->form = new ModelAForm($model_a);
$this->form->bind($request->getParameter($this->form->getName()));
if($this->form->isValid()) {
$this->form->save();
}
}