php - cakephp 中主键字段的唯一性未得到验证

标签 php mysql cakephp

我正在开发cakephp框架,其中mysql是后端。我正在开发表单来输入产品详细信息,其中product_id需要手动添加。我已经编写了product_id字段的验证规则。它适用于其他验证规则,即对于非空,数字。但不适用于唯一验证规则。这是我在模型中的验证代码:

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')));

我无法指出错误位置。

最佳答案

不应该是:

var $validate= array('product_id'=> array('rule'=>'numeric',
                    'on'=>'create',
                     //etc

否则您是否正在尝试验证名为numeric的字段?

此外,虽然我不知道您的应用程序如何运行,但使用 product_id 听起来应该与 products 表中的另一条记录相关 - 这将自动处理 Cake,即在选择中。

如果您要输入唯一代码(例如内部引用号),请考虑将该字段重命名为不太像 Cake 的名称,以防止可能出现的任何问题或与其他 Cake 开发人员混淆。

关于php - cakephp 中主键字段的唯一性未得到验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6562842/

相关文章:

mysql - 错误 : Error 1215: Cannot add foreign key constraint when no foreign constraint being added

mysql - 在 SQL 中添加多个替代 WHERE 子句

php - 搜索结果错误

cakephp - 如何在CakePHP上测试HTML电子邮件模板?

php - Codeigniter URI 路由和安全

php - 如何使用PHP打印此模式?

php - MySQL 不允许在递归公用表表达式中使用 LIMIT

php - 停止来自 php 函数 gzuncompress 的错误消息

php - Security::hash() 不返回相同的哈希值

cakephp - CakePHP 2.0 中的错误处理。转义 View 变量