主键字段未获得在cakephp中的唯一性验证 [英] Primary key field is not getting validated for uniqueness in cakephp
问题描述
我正在开发cakephp框架,其中mysql是后端。我开发的形式输入产品详细信息,其中product_id是手动添加。我已经写了product_id字段的验证规则。它适用于其他验证规则ie对于非空的,numeric.But不能用于唯一的验证规则。这是我的验证代码在模型中:
I am working on cakephp framework,where mysql is back end.I am developing form to enter product details,where product_id is to be added manually.I have written the validation rules for product_id field.It works for other validation rules i.e. for not empty, numeric.But not working for unique validation rule.Here is my validation code in the model:
var $validate= array(product_id=>
'numeric'=> array('rule'=>'numeric',
'on'=>'create',
'message'=>'Please enter only numbers'),
'isUnique' => array('rule' => 'isUnique',
message' => 'This product_id already exist'),
'notEmpty'=> array('rule' => 'notEmpty',
message' => 'Please enter the product id')),
'product_name' => array('notEmpty'=> array(
'rule' => 'notEmpty',
'message' => 'Please enter the product name')));
我无法指出错误位置。
I couldn't able to point out the error location.
推荐答案
不应该是:
var $validate= array('product_id'=> array('rule'=>'numeric',
'on'=>'create',
//etc
否则您正在尝试验证 numeric
?
此外,虽然我不知道你的应用程序的功能,使用 product_id
与 products
表中的另一条记录相关联,这将在Cake中自动处理,即在select中。
Also, whilst I don't know how your app functions, using product_id
sounds like it should relate to another record, in the products
table - which would be automatically dealt with with Cake i.e. in a select.
如果您要输入唯一的代码(例如内部参考号),请考虑将该字段重命名为不像Cake-like,以防止可能出现的任何问题,或与其他Cake开发人员混淆。
If you are entering a unique code (such as an internal Reference number), consider renaming the field to something less-Cake-like to prevent any issues that may arise, or confusion with another Cake developer.
这篇关于主键字段未获得在cakephp中的唯一性验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!