如何存储来自实体类型选择框(Symfony)的数据?
How can I store data from entity type selectbox (Symfony)?
我有一个用 formbuilder 创建的 select 框:
<select id="form_type" name="form[type]" class="form-control select2 select2-hidden-accessible" tabindex="-1" aria-hidden="true">
<option value="1">text</option>
<option value="2">hidden</option>
<option value="3">password</option>
<option value="4" selected="selected">icon</option>
<option value="5">select</option>
</select>
我的控制器是这样创建的:
$formBuilder->add('type', EntityType::class, array(
'attr' => array('class' => 'form-control select2'), 'label' => 'Type',
'class' => FieldTypes::class,
'choice_label' => function ($fieldTypes) {
return $fieldTypes->getName();
}
));
$formBuilder->add('cancel', ButtonType::class, array('label' => 'cancel','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar')))
->add('save', SubmitType::class, array('label' => 'Save','attr' => array('id' => 'submit-my-beautiful-form','class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')));
$form = $formBuilder->getForm();
$form->handleRequest($request);
但是当我想保存一个 selection 时,我收到错误消息:
Argument 1 passed to App\Entity\Fields::setType() must be an instance
of App\Entity\FieldTypes or null, string given, called in
/Users/work/project/src/Controller/PagesController.php on line 242
在我的实体中:
public function setType(?FieldTypes $type): self
{
$this->type = $type;
return $this;
}
这是它的存储方式:
$entity = $this->getDoctrine()->getRepository($EntityName)->find($data['form[id]']);
$em = $this->getDoctrine()->getManager();
foreach ($fields as $field) {
$em = $this->getDoctrine()->getManager();
$func = 'set'.$field['fieldName'];
$args = $data['form['.$field['fieldName'].']'];
$entity->$func($args);
}
$em->flush();
使用方法$form->handleRequest($request)
。它将使用其配置填充您的表单(它将使用发布的 ID 实例化 FieldType
)。
在这里,您手动添加 select 的 "raw" 值,这是包含 FieldType
.
的 ID 的字符串
编辑:参见 docs
根据你的代码,有点难理解,但是
如果 $field 是一个字符串。
foreach ($fields as $field) {
$obj = $this->getDoctrine()->getRepository($fieldEntity)->find(field);
$entiy->setType($obj);
}
在表单提交块后尝试
$entity->setType($form->get('type'));
$form->get('type') 将 return 作为 FieldTypes class.
同样,如果您使用 $field 而不是 $field['fieldName'],
会反对 return。
例如,
$func = 'set'.$field;
我有一个用 formbuilder 创建的 select 框:
<select id="form_type" name="form[type]" class="form-control select2 select2-hidden-accessible" tabindex="-1" aria-hidden="true">
<option value="1">text</option>
<option value="2">hidden</option>
<option value="3">password</option>
<option value="4" selected="selected">icon</option>
<option value="5">select</option>
</select>
我的控制器是这样创建的:
$formBuilder->add('type', EntityType::class, array(
'attr' => array('class' => 'form-control select2'), 'label' => 'Type',
'class' => FieldTypes::class,
'choice_label' => function ($fieldTypes) {
return $fieldTypes->getName();
}
));
$formBuilder->add('cancel', ButtonType::class, array('label' => 'cancel','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar')))
->add('save', SubmitType::class, array('label' => 'Save','attr' => array('id' => 'submit-my-beautiful-form','class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')));
$form = $formBuilder->getForm();
$form->handleRequest($request);
但是当我想保存一个 selection 时,我收到错误消息:
Argument 1 passed to App\Entity\Fields::setType() must be an instance of App\Entity\FieldTypes or null, string given, called in /Users/work/project/src/Controller/PagesController.php on line 242
在我的实体中:
public function setType(?FieldTypes $type): self
{
$this->type = $type;
return $this;
}
这是它的存储方式:
$entity = $this->getDoctrine()->getRepository($EntityName)->find($data['form[id]']);
$em = $this->getDoctrine()->getManager();
foreach ($fields as $field) {
$em = $this->getDoctrine()->getManager();
$func = 'set'.$field['fieldName'];
$args = $data['form['.$field['fieldName'].']'];
$entity->$func($args);
}
$em->flush();
使用方法$form->handleRequest($request)
。它将使用其配置填充您的表单(它将使用发布的 ID 实例化 FieldType
)。
在这里,您手动添加 select 的 "raw" 值,这是包含 FieldType
.
编辑:参见 docs
根据你的代码,有点难理解,但是 如果 $field 是一个字符串。
foreach ($fields as $field) {
$obj = $this->getDoctrine()->getRepository($fieldEntity)->find(field);
$entiy->setType($obj);
}
在表单提交块后尝试
$entity->setType($form->get('type'));
$form->get('type') 将 return 作为 FieldTypes class.
同样,如果您使用 $field 而不是 $field['fieldName'], 会反对 return。 例如,
$func = 'set'.$field;