2012-07-05 131 views
1

即時消息我的數據庫我有一個折扣表,用戶對特定「範圍」,「類別」,「供應商」和產品類型進行折扣。我如何讓cakephp不允許多個「折扣」?cakephp 2.0獨特的多場驗證

如折扣

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

幾乎一樣http://stackoverflow.com/questions/11173835/cakephp-2-1-custom-validation-rule-check-for-unique-field-valu電子組合/ 11175385#comment14663880_11175385 - 請參閱可能的解決方案的答案 – mark 2012-07-05 12:31:41

+1

重複http://stackoverflow.com/questions/2461267/cakephp-isunique-for-2-fields/2464201#2464201(請參閱我的答案) – RichardAtHome 2012-07-05 13:12:10

+0

感謝您的快速回放和答案,這很好:) – 2012-07-05 15:30:45

回答

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