问题描述
我已经在我的 symfony 表单中设置了一个后验证器来停止主键的重复.
I've setup a post validator in my symfony form to stop duplication of primary keys.
在这种情况下,主键是一个两个字符的字符串.用于验证的代码:
A primary key is a two-character string in this instance. Code used to validate:
$this->mergePostValidator(new sfValidatorDoctrineUnique(array(
'model' => 'Manufacturers',
'column' => 'id',
'primary_key' => 'id'
)));
主键是大写的(例如 AU).奇怪的是,后验证器成功触发的是小写的au"被输入到字段中(即阻止它进入数据库并触发 500 完整性约束错误),但如果正确输入为AU",它似乎没有注意到重复.
The primary key is uppercase (for example AU). Bizarrely the post validator triggers successfully is lowercase 'au' is entered into the field (i.e. stops it from going to the database and triggering a 500 integrity constraint error), but if entered correctly as 'AU' it doesn't seem to notice the duplication.
有什么想法吗?
推荐答案
这不是 symfony sfDoctrineValidator 的问题.这个验证器所做的就是在您的数据库中搜索现有记录.如果您使用_ci"(不区分大小写)排序规则(您是否使用 mysql?),搜索将不返回任何内容 - 验证器被愚弄了.
That's not a symfony sfDoctrineValidator issue. All this validor does is to search your database for an existing record. If you are using a "_ci" (case-insensitive) collation (are you using mysql?) the search returns nothing - the validator is fooled.
然后当你插入副本时,你会从数据库中得到一个异常.尝试像这样更改表格的排序规则:
Then when you insert the duplicate, you get a exception from the database. Try to change the collation of your table like this:
ALTER TABLE `table` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
(你应该告诉教义为你做这件事:
(you should tell doctrine to do it for you:
MyTable:
options: { collate: utf8_bin, charset: utf8 }
)
这篇关于sfValidatorDoctrineUnique 在大写字母上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!