symfony - SELECT 的 Symfony 值
问题描述
我有一个 Select 获取字符串列表,但我需要在数据库中插入他们的 ID,而不是他们的名字。我在 symfony 4.2 下工作 在我的表单中,我使用这个(有效):
$builder
->add('cours', EntityType::class, array(
'class' => Cours::class,
'label' => false,
'placeholder' => 'Cours',
'required' => true,
'choice_label' => 'nomUe',
'choices' => $formation->getCours(),
'attr' => array('class' => 'custom-select')
))
'choice_value' => $formation->getId(),
和想法?谢谢!
解决方案
实际上,您可能已经在 Class 实体中重新定义了 __toString 方法……这就是为什么您在表单中具有可视名称的原因……但是在数据库中它将记录您的实体的 ID。
所以你必须删除表单中的行选择......所以最后你的表单应该是这样的:
$builder
->add('cours', EntityType::class, array(
'class' => Cours::class,
'label' => false,
'placeholder' => 'Cours',
'required' => true,
'choice_label' => 'nomUe',
'attr' => array('class' => 'custom-select')
))
但是...我想您在 Class 实体中的 __toString 方法如下所示:
public function __toString()
{
return $this -> getName() ;
}
因此,如果您更喜欢在表单中使用 Id 而不是实体的名称,则必须显示它:
public function __toString()
{
return (String) $this -> getId() ;
}
推荐阅读
- input - 被键盘遮挡的文本框
- algorithm - 具有非成对互质模量的同余系统
- r - 如何在 R 中较新版本的 igraph 中实现 sample_ 和 sbm() 函数?
- java-stream - 我能说java.util.stream.Stream中的peek()方法应该是幂等的吗
- pipelinedb - 是否有适用于 Laravel 或 Native PHP 的 PipelineDB 包?
- linux - 我的 bash 脚本中的结构错误:无法使用通配符正确列出目录中的文件
- angularjs - AngularJS - 表内行的ng-repeat
- android - 如果用户有使用 firebase 的 Internet 连接,请在线检查用户
- ssl - nghttp2 api.binance.com ssl 错误
- android - Android Studio 错误膨胀约束布局