summerblue/administrator

Enum下拉框的存储有点小问题

shadylplplp opened this issue · 3 comments

https://github.com/summerblue/administrator/blob/master/docs/field-type-enum.md
里面写了
You can either provide a simple array of strings, or if the key is a string, the key will be saved to the database while the value will be presented to the user.
您可以提供一个简单的字符串数组,或者如果键是字符串,则键将保存到数据库中,而值将呈现给用户。

如果array的key是string型的 数据库是整型integer的 是无法成功保存的
例如:
'state'=>[
'title'=>'审核',
'type'=>'enum',
'options'=>array('1'=>'未审核','2'=>'过审核','3'=>'不过审核'),
],

但如果没有键的数组 即使值是string型的 也能保存进整型integer的数据库

例如:
'state'=>[
'title'=>'审核',
'type'=>'enum',
'options'=>array('1','2','3'),
],

额我发现这行话都是错的
或者如果键是字符串,则键将保存到数据库中,而值将呈现给用户。
即使键是字符串 也不会把键保存到数据库 而是把值保存到数据库了

多检查了几次 好像是因为数组的key是数字导致的 如果是别的字符就正常了
array('1'=>'未审核','2'=>'过审核','3'=>'不过审核'),

最后发现是Enum.php里面build()函数判断的问题

'id' => is_numeric($val) ? $text : $val,
改成
'id' => is_string($val) ? $text : $val,
这样就可以解决