1

我的数据库我有一个折扣表,用户为特定的“范围”、“类别”、“供应商”和产品类型打折。我如何让 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' => ''
        )
    );


}
4

1 回答 1

1

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.'
            )
        ),
    );
于 2016-03-31T09:36:36.957 回答