1

im my database i have a discounts table, a user makes a discount for a specific "range","category","supplier" and product type. How do i make cakephp not allow multiple "discounts"?

eg a discount is

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

How to i ensure that another discount cant be created for that supplier,range,category and product type?

My Discount model has only one relation ship in it (not sure if that makes a difference)

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


}
fuzzy dunlop
  • 477
  • 2
  • 10
  • 22
  • 1
    pretty much the same as http://stackoverflow.com/questions/11173835/cakephp-2-1-custom-validation-rule-check-for-unique-field-value-combination/11175385#comment14663880_11175385 - see the answer for a possible solution – mark Jul 05 '12 at 12:31
  • 1
    Duplicate of http://stackoverflow.com/questions/2461267/cakephp-isunique-for-2-fields/2464201#2464201 (see my answer there) – RichardAtHome Jul 05 '12 at 13:12
  • thanks for the quick replay and answers, that worked great:) – fuzzy dunlop Jul 05 '12 at 15:30

1 Answers1

1

It's very simple to handle unique in cakephp with having multiple fields

In Model, for the discount validation rules like:

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.'
            )
        ),
    );
A J
  • 3,970
  • 14
  • 38
  • 53