如果我提出一个可能已经被问到的问题,我事先表示歉意,但是我认为没有发现任何结果。
我的以下CakePHP代码部分有问题:
$unavailable_resource_ids = $this->Resources->Unavailabilities
->find()
->select(['resource_id'])
->matching('Bookings', function ($q) {
return $q->where([
'Bookings.booking_status_id NOT IN ' => [6, 4, 3]
]);
})
->where([
'OR' => [
[
'Unavailabilities.start <= :start_date',
'DATE_ADD(Unavailabilities.end, INTERVAL '. $recovery_time .') >= :end_date'
],
[
'Unavailabilities.start <= :start_date',
'DATE_ADD(Unavailabilities.end, INTERVAL '. $recovery_time .') BETWEEN :start_date AND :end_date'
],
[
'Unavailabilities.start BETWEEN :start_date AND :end_date',
'DATE_ADD(Unavailabilities.end, INTERVAL '. $recovery_time .') >= :end_date'
],
[
'Unavailabilities.start >= :start_date',
'DATE_ADD(Unavailabilities.end, INTERVAL '. $recovery_time .') <= :end_date'
]
]
])
->bind(':start_date', $start_date_object, 'datetime')
->bind(':end_date', $end_date_object, 'datetime');
我需要为变量recovery_time
提供适当的值,该变量将从资源表中提取,但是我无法解决该问题。
表resources
列:
id, description, model, recovery_time, etc....
我需要将recovery_time
值插入变量recovery_time
中,该变量可以是1天,1周等。
我有一个 解决方案,我在INTERVAL中添加了一个子查询,下面是代码,也许对其他人有用。
$unavailable_resource_ids = $this->Resources->Unavailabilities
->find()
->select(['resource_id'])
->matching('Bookings', function ($q) {
return $q->where(['Bookings.booking_status_id NOT IN ' => [6, 4, 3]]);
})
->where([
'OR' => [
['Unavailabilities.start <= :start_date', 'DATE_ADD(Unavailabilities.end, INTERVAL (SELECT resources.recovery_time FROM resources WHERE id = Unavailabilities.resource_id) DAY) >= :end_date'],
['Unavailabilities.start <= :start_date', 'DATE_ADD(Unavailabilities.end, INTERVAL (SELECT resources.recovery_time FROM resources WHERE id = Unavailabilities.resource_id) DAY) BETWEEN :start_date AND :end_date'],
['Unavailabilities.start BETWEEN :start_date AND :end_date', 'DATE_ADD(Unavailabilities.end, INTERVAL (SELECT resources.recovery_time FROM resources WHERE id = Unavailabilities.resource_id) DAY ) >= :end_date'],
['Unavailabilities.start >= :start_date', 'DATE_ADD(Unavailabilities.end, INTERVAL (SELECT resources.recovery_time FROM resources WHERE id = Unavailabilities.resource_id) DAY) <= :end_date']
]
])
->bind(':start_date', $start_date_object, 'datetime')
->bind(':end_date', $end_date_object, 'datetime');`