我有一个独特的字段模型,如下图所示。问题是这个字段是可选的,当它为空时,我得到了教条的唯一错误信息。Doctrine PHP 1.2 - 如何跳过空字段的唯一验证
我期待它只验证'notnull'=> true字段的唯一性。
$this->hasColumn('filename', 'string', 40, array(
'type' => 'string',
'unique' => true,
'length' => '40',
));
在此先感谢您。
编辑:我禁用验证,它看起来像外地携带一个空字符串,而不是空的:
SQLSTATE [23000]:完整性约束冲突:“关键“文件名”
1062重复条目”所以现在的问题是:如何在空白值上强制使用空值...
编辑2:我是这样做的解决方法。 =/
public function preValidate()
{
if(!$this->filename) {
$this->filename = null;
}
}
我假设你正在使用symfony 1.4。否则,这个过程仍然非常相似。 – Slickrick12
伟大的解决方案。但是为了工作,我换成了$ column = $ this-> getOption('column'); $ column = current($ this-> getOption('column')); beacuse getColumn返回一个数组。 –