【问题标题】:Cakephp 3 json result query on datetimeCakephp 3 json结果查询日期时间
【发布时间】:2016-06-15 14:33:57
【问题描述】:

这是我的 derp 时刻,卡住了...我有一个 ajax 调用去控制器操作,我有一个比较两个日期戳的查询,我收到以下错误:

前端js循环和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>';
                    $('#tourDetails').append(html);
                        tourtime.setMinutes(tourtime.getMinutes() + 30);                        
                    console.log(tourtime);

                    count++;
                }

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


                  });
            });

错误和后端显示更多关于正在发生的事情。我想我的日期比较错误,但我不确定在哪里......

    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]
    Trace:
    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
        ]
    ])->all()
    ;

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

    }

【问题讨论】:

  • 错误是否说明是哪一行导致了转换错误?但是当您有 $foo = array(); echo $foo;$bar = 'this will end well' . $foo 之类的东西时会发生确切的错误 - 在字符串上下文中使用数组。
  • 我认为是查询,传入的是2016-07-17T15:00:00-05:00
  • 以下是响应中附加的错误消息: 注意 (8):数组到字符串的转换 [CORE/src/ORM/Table.php,第 1782 行] 警告 (2):strtolower() 需要参数1为字符串,给定数组[CORE/src/ORM/BehaviorRegistry.php,第215行]注意(8):数组到字符串转换[CORE/src/ORM/Table.php,第1792行]
  • 无论何时收到错误,请始终发布 complete 错误消息,包括 full 堆栈跟踪(最好从日志中正确复制可读时尚)在您的问题中,即使问题对于熟悉 CakePHP 的人来说可能很明显!另外,请务必提及您的 准确 CakePHP 版本 (x.x.x)!
  • 我知道,但这不是 确切 版本,确切的版本号由三个数字组成,例如 3.2.1(检查 vendor/cakephp/cakephp/VERSION.txt)。在不知道确切版本的情况下,很难弄清楚错误消息指的是什么代码。理想情况下,您也可以将 that 放在您的问题中。

标签: php json ajax cakephp cakephp-3.0


【解决方案1】:

Table::find() 需要一个字符串作为第一个参数,即查找类型。您正在传递一个数组,因此会出现错误。

http://api.cakephp.org/3.2/class-Cake.ORM.Table.html#_find

通过仔细检查错误消息并检查相应的代码,应该很容易发现此类错误。理想情况下,查看堆栈跟踪并检查代码流。

除此之外,您的条件是错误的,您在那里调用 PHP date() 函数,这最终也会导致错误,因为您将日期字符串作为列传递,这当然不会存在。你的情况应该是这样的

'Orders.booking_date_time' => $date_time

此外,不必手动格式化时间,您应该能够简单地从中创建一个 \DateTime\Cake\I18n\Time 实例,例如

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

$date_time 是原始输入。

还有一种不相关的东西,不要直接访问像$_POST 这样的超全局变量!这只会导致问题,至少在测试环境中是这样。使用请求对象检索输入,例如$this-&gt;request-&gt;data('date_time')

另见

【讨论】:

  • 感谢您的帮助,这太糟糕了。并感谢有关不直接访问 $post 的建设性信息。!欢呼
猜你喜欢
  • 1970-01-01
  • 2020-08-09
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多