CakePhp IsUnique模型中的规则

时间:2019-01-31 15:51:34

标签: cakephp model unique

我正在建立一个有时间表进行约会的系统,我有一个带有字段的表:

user_id         : user id from the person who will reserve an hour.
day_from_1_to_7 : day from 1 (Monday) to 7 (Sunday)
time            : It could be from 09:00, 10:00 to 21:00 hours.
room            : number, it is the room that it will be taken the date.

我需要两次验证:

验证1:

If there is a record already inserted with:

    user_id         = 1
    day_from_1_to_7 = 1 (monday)
    time            = 09:00
    room            = 3

Then it can not allow same record but with different room:

    user_id         = 1
    day_from_1_to_7 = 1 (monday)
    time            = 09:00
    room            = 2

This is because The user_id=1 is already attending at monday 09:00 in another room so it can not be allow to take same time in another room.
In other words I require to be unique by ('user_id', 'day_from_1_to_7', 'time')

验证1:

If there is a record already inserted with:

    user_id         = 4
    day_from_1_to_7 = 2 (tuesday)
    time            = 09:00
    room            = 3
Then it can not allow same record but with different user_id:

    user_id         = 1
    day_from_1_to_7 = 2 (tuesday)
    time            = 09:00
    room            = 3

This is because The user_id=4 is already attending at tuesday 09:00 in same room so it can not be allow to take another user_id in same time, day and room.
In other words I require to be unique by ('day_from_1_to_7', 'time', 'room')

我在模型中都添加了这两个规则,但是它们不能正常工作。我加错了吗?你能帮我吗?

public $validate = array
(
    ...//more rules
    ,'user_id' => array(
                        'rule' => array('isUnique', array('user_id', 'day_from_1_to_7', 'time'), false),
                        'message' => 'Same user is already attending another room at same time.'
                        )
    ,'user_id' => array(
                        'rule' => array('isUnique', array('day_from_1_to_7', 'time', 'room'), false),
                        'message' => 'Another user is attending in this room at this time.'

                        )
)

0 个答案:

没有答案