I apologize in advance if I am opening a question that may have already been asked, but I don't think I have found any results.
I have a problem with the following CakePHP code portion:
$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');
I need to provide the appropriate value to the variable recovery_time
, which will be extracted from the resources table, however I have not been able to resolve the issue.
Table resources
column:
id, description, model, recovery_time, etc....
I need the recovery_time
value to be inserted into the variable recovery_time
, which can be 1 day, 1 week etc.
I got a solution, i have added a sub query into the INTERVAL, here is the code, maybe can be useful for someone else.
$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');`
来源:https://stackoverflow.com/questions/55630252/cakephp-3-7-5-retrive-data-of-a-field-into-the-controller