cakephp 2.0独特的多字段验证

时间:2012-07-05 11:59:21

标签: cakephp cakephp-2.0

我的数据库我有折扣表,用户为特定的“范围”,“类别”,“供应商”和产品类型打折。如何让cakephp不允许多次“折扣”?

例如折扣是

user_id 100
category_id 1 
range_id 2 
producttype "doors"
discount 10%

如何确保不能为该供应商,范围,类别和产品类型创建另一个折扣?

我的折扣模型中只有一个关系(不确定是否会产生影响)

<?php
App::uses('AppModel', 'Model');
/**
 * Discount Model
 *
 * @property User $User
 */
class Discount extends AppModel {
/**
 * Validation rules
 *
 * @var array
 */
 //public $displayField = 'discount';

    public $validate = array(
        /*'title' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),*/
        'user_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'discount' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );


}

1 个答案:

答案 0 :(得分:0)

使用多个字段来处理cakephp中的唯一

非常简单

在模型中,对于折扣验证规则,例如:

public $validate = array(
        'discount' => array(
            'numeric' => array(
                'rule' => array('numeric'),

            ),
            'isUnique' => array(
                'rule' => array('isUnique',array('user_id','category_id','range_id','producttype'),false),
                'message' => 'discount already Exist.'
            )
        ),
    );