I have 'accommod_units'
and 'accommod_bookings'
table and I want to check the availability of accommodation units for a certain period. For example, Date of arrival: 2019-08-20 and Departure date: 2019-08-27.
'accommod_bookings'
table is shown below:
This code does not work:
$this->loadModel('AccommodUnits');
$this->conditionBookings = [
'NOT' => [
[
'AccommodBookings.date_start >=' => '2019-08-20',
'AccommodBookings.date_end <=' => '2019-08-27'
],
]
];
$accommodUnits = $this->AccommodUnits
->find('all')
->distinct(['AccommodUnits.id'])
->contain([
'AccommodBookings' => function ($q) {
return $q->where($this->conditionBookings);
},
])
->where($conditionUnits)
->matching('AccommodBookings', function ($q) {
return $q->where($this->conditionBookings);
});
Their associations are:
$accommodUnits->hasMany('AccommodBookings');
How to solve this problem?