This is my table event
As you see
Transaction 1
time_begin: 2016-03-10 07:00:00
time_end: 2016-03-10 08:00:00
place: 1
If user makes a Transaction 2 and then selects
time_begin: 2016-03-10 07:15:00
time_end: 2016-03-10 07:45:00
place: 1
How to make a custom validation rule that checks datetime and place is not used in another transaction?
So I have tried this:
// models.php
public function rules()
{
return [
[['title', 'description', 'time_begin', 'time_end', 'place'], 'required'],
[['description', 'status'], 'string'],
[['time_begin', 'time_end'], 'safe'],
[['title'], 'string', 'max' => 150],
[['place'], 'string', 'max' => 50],
[['remark'], 'string', 'max' => 145],
[['time_end'], 'compare','compareAttribute'=>'time_begin','operator'=>'>','message'=>''],
[['place', 'time_begin'], 'unique', 'targetAttribute' => ['place', 'time_begin'],'message'=>'time and place is busy place select new one.'],
[['place'], 'checkDate','skipOnEmpty' => false],
];
}
public function checkDate($attribute, $params)
{
$a = Event::find()->where(['>=', 'time_begin', ($this->time_begin)])->count();
$b = Event::find()->where(['<=', 'time_end', ($this->time_end)])->count();
$c = Event::find()->where(['=', 'place', ($this->place)])->count();
$d = ($this->time_begin);
$e = ($this->time_end);
$f = ($this->place);
if (($d > $a) && ($f = $c))
{
$this->addError($attribute, 'This place and time is busy, selcet new place or change time.');
}
}
It won't work because this if (($d > $a) && ($f = $c))
is wrong.
But I don't know how to write the condition for check that thing.