Brenton Pierce Brenton Pierce - 1 year ago 191
PHP Question

Cakephp 3 json result query on datetime

Here is my derp moment, stuck... I have an ajax call going to a controller action and I have a query comparing two datestamps and I get the below error:

front end js loop and ajax

while(count <= toursToday){
tourtimeFormatted = moment.utc(tourtime);
var html = '<button data-id="<?= $tour->id ?>" data-timeslot="'+tourtimeFormatted+'" class="btn btn-default timeSlot">'+formatAMPM(tourtime)+'</button>';
tourtime.setMinutes(tourtime.getMinutes() + 30);


$('body').on('click', 'button.timeSlot', function() {
var time = $(this).data('timeslot');
var tour_id = $(this).data('id');
//ajax get avaliable seats
method: "POST",
url: "/getseats.json",
data: { tour_id: tour_id, date_time: time }
.success(function( data ) {


Error and Backend showing more about what is happening. I think I have the dates comparison wrong, but I'm not sure where...

2016-06-15 14:56:29 Error: [BadMethodCallException] Unknown finder method "Array"
Request URL: /getseats.json
Referer URL: http://paddletap.lan/book-a-tour/6
Stack Trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/PaddleTapRedesign/vendor/cakephp/cakephp/src/ORM/Table.php(943): Cake\ORM\Table->callFinder(Array, Object(Cake\ORM\Query), Array)
#1 /Applications/XAMPP/xamppfiles/htdocs/PaddleTapRedesign/src/Controller/TourController.php(98): Cake\ORM\Table->find(Array)
#2 [internal function]: App\Controller\TourController->getseats()
#3 /Applications/XAMPP/xamppfiles/htdocs/PaddleTapRedesign/vendor/cakephp/cakephp/src/Controller/Controller.php(429): call_user_func_array(Array, Array)
#4 /Applications/XAMPP/xamppfiles/htdocs/PaddleTapRedesign/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(114): Cake\Controller\Controller->invokeAction()
#5 /Applications/XAMPP/xamppfiles/htdocs/PaddleTapRedesign/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(87): Cake\Routing\Dispatcher->_invoke(Object(App\Controller\TourController))
#6 /Applications/XAMPP/xamppfiles/htdocs/PaddleTapRedesign/webroot/index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#7 {main}

2016-06-15 15:08:23 Warning: Warning (2): strtolower() expects parameter 1 to be string, array given in [/Applications/XAMPP/xamppfiles/htdocs/PaddleTapRedesign/vendor/cakephp/cakephp/src/ORM/BehaviorRegistry.php, line 215]
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 147
strtolower - [internal], line ??
Cake\ORM\BehaviorRegistry::hasFinder() - CORE/src/ORM/BehaviorRegistry.php, line 215
Cake\ORM\Table::callFinder() - CORE/src/ORM/Table.php, line 1787
Cake\ORM\Table::find() - CORE/src/ORM/Table.php, line 943
App\Controller\TourController::getseats() - APP/Controller/TourController.php, line 98
Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php, line 429
Cake\Routing\Dispatcher::_invoke() - CORE/src/Routing/Dispatcher.php, line 114
Cake\Routing\Dispatcher::dispatch() - CORE/src/Routing/Dispatcher.php, line 87
[main] - ROOT/webroot/index.php, line 37

public function getseats()
$orders = TableRegistry::get('Orders');

$tour_id = $_POST['tour_id'];
$date_time = $_POST['date_time'];
$new_date = date('Y-m-d H:i:s', strtotime($date_time));

//find order items for tour id
$bookings = $orders->find([
'conditions' => [
DATE('Orders.booking_date_time') => $date_time

//where date is M-d-yy
//where time is H:i
$this->set('bookings', $bookings);
$this->set('_serialize', ['bookings']);


ndm ndm
Answer Source

Table::find() expects a string for the first argument, the find type. You are passing an array, hence the error.

Such errors should be rather easy to figure by closely inspecting the error message, and checking the corresponding code. Ideally, look at the stacktrace and check the code flow.

Besides that, your conditions are wrong, you are calling the PHP date() function there, which in the end would cause an error too, as you'd pass a date string as the column, which of course doesn't exist. Your condition should be like

'Orders.booking_date_time' => $date_time

Also it should not be necessary to manually format the time, you should be able to simply create a \DateTime or \Cake\I18n\Time instance from it, like

'Orders.booking_date_time' => new \DateTime($date_time)

where $date_time is the raw input.

And another, kind of unrelated thing, do not access superglobals like $_POST directly! This will just cause problems, at the very least in the test environment. Use the request object to retrieve input, like $this->request->data('date_time').

See also

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download