Правила CakePhp IsUnique в модели - PullRequest
0 голосов
/ 31 января 2019

Я строю систему с расписанием для назначения встреч, у меня есть таблица с полями:

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.'

                        )
)
...