我正在使用CakePHP 3.4
我使用
从LogProfileVisits
表中获取访问者数量
$profile_visit_logs = $this->Users->LogProfileVisits->find()
->where(['user_id' => $user->id]);
$profile_visit_logs->select([
'total_count' => $profile_visit_logs->func()->count('id'),
'unique_count' => $profile_visit_logs->func()->count('DISTINCT `ip_address`'),
'visit_date' => 'MONTH(created)'
])
->group('visit_date')
->limit(10);
但这会导致致命错误
Error: Call to a member function format() on boolean
File /path_to_app/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php
Line: 161
同样适用于DATE(created)
和YEAR(created)
错误日志
2017-06-02 12:25:45 Warning: Warning (2): DateTimeImmutable::modify() [<a href='http://php.net/datetimeimmutable.modify'>datetimeimmutable.modify</a>]: Failed to parse time string (5) at position 0 (5): Unexpected character in [/path_to_app/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php, line 160]
Request URL: /dashboard/profile/ajax_profile_performance
Referer URL: http://example.com/dashboard
Client IP: client_ip
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 153
DateTimeImmutable::modify() - [internal], line ??
Cake\Chronos\Date::modify() - ROOT/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php, line 160
Cake\Database\Type\DateTimeType::toPHP() - CORE/src/Database/Type/DateTimeType.php, line 139
Cake\Database\Type\DateType::toPHP() - CORE/src/Database/Type/DateType.php, line 90
Cake\Database\FieldTypeConverter::__invoke() - CORE/src/Database/FieldTypeConverter.php, line 78
Cake\Database\Statement\CallbackStatement::fetch() - CORE/src/Database/Statement/CallbackStatement.php, line 59
Cake\ORM\ResultSet::_fetchResult() - CORE/src/ORM/ResultSet.php, line 484
Cake\ORM\ResultSet::valid() - CORE/src/ORM/ResultSet.php, line 276
App\Controller\Dashboard\ProfileController::ajaxProfilePerformance() - APP/Controller/Dashboard/ProfileController.php, line 113
Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php, line 440
Cake\Http\ActionDispatcher::_invoke() - CORE/src/Http/ActionDispatcher.php, line 119
Cake\Http\ActionDispatcher::dispatch() - CORE/src/Http/ActionDispatcher.php, line 93
Cake\Http\BaseApplication::__invoke() - CORE/src/Http/BaseApplication.php, line 78
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Routing\Middleware\RoutingMiddleware::__invoke() - CORE/src/Routing/Middleware/RoutingMiddleware.php, line 59
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Routing\Middleware\AssetMiddleware::__invoke() - CORE/src/Routing/Middleware/AssetMiddleware.php, line 88
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Error\Middleware\ErrorHandlerMiddleware::__invoke() - CORE/src/Error/Middleware/ErrorHandlerMiddleware.php, line 92
Cake\Http\Runner::__invoke() - CORE/src/Http/Runner.php, line 65
Cake\Http\Runner::run() - CORE/src/Http/Runner.php, line 51
Cake\Http\Server::run() - CORE/src/Http/Server.php, line 80
[main] - ROOT/webroot/index.php, line 37
2017-06-02 12:25:45 Error: Fatal Error (1): Call to a member function format() on boolean in [/path_to_app/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php, line 161]
Request URL: /dashboard/profile/ajax_profile_performance
Referer URL: http://profplus.in/dashboard
Client IP: client_ip
Trace:
Cake\Error\BaseErrorHandler::handleFatalError() - CORE/src/Error/BaseErrorHandler.php, line 223
Cake\Error\BaseErrorHandler::Cake\Error\{closure}() - CORE/src/Error/BaseErrorHandler.php, line 105
[main] - [internal], line ??
2017-06-02 12:25:46 Warning: Headers already sent in /path_to_app/vendor/cakephp/cakephp/src/Error/Debugger.php:822
2017-06-02 12:25:46 Error: [Cake\Error\FatalErrorException] Call to a member function format() on boolean in /path_to_app/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php on line 161
Request URL: /dashboard/profile/ajax_profile_performance
Referer URL: http://example.com/dashboard
Client IP: client_ip
Stack Trace:
#0 /path_to_app/vendor/cakephp/cakephp/src/Error/BaseErrorHandler.php(105): Cake\Error\BaseErrorHandler->handleFatalError(1, 'Call to a membe...', '/home/thebornen...', 161)
#1 [internal function]: Cake\Error\BaseErrorHandler->Cake\Error\{closure}()
#2 {main}
答案 0 :(得分:0)
尝试使用此代码,以便知道要使用的模型:
$this->loadModel('LogProfileVisits');
答案 1 :(得分:0)
查看堆栈跟踪,ORM似乎将visit_date
列视为DATE
类型,您可以通过调用DateType::toPHP()
的行来判断:
Cake\Database\Type\DateType::toPHP() - CORE/src/Database/Type/DateType.php, line 90
这是事情开始出错的地方,数字5
(MONTH(created)
的结果)不是有效的日期时间值,因此您最终会得到null
value,然后在尝试调用方法时导致致命错误:
警告(2):DateTimeImmutable :: modify()[...]无法解析时间字符串(5)
DATE()
或YEAR()
的结果是有效的,前者显然会返回一个可解析的日期,后者一年,这可能不是很明显,但也可以解析,它会是虽然被视为时间值,但例如2017
将被解释为20:17
。你最终会得到一个无用的/错误的日期对象,但它不会出错。
<强> http://www.php.net/manual/en/datetimeimmutable.modify.php 强>
总之,您需要弄清楚为什么ORM将列视为DATE
类型。这肯定不是默认值,默认情况下,计算/虚拟列将被视为字符串。如果visit_date
不是您在评论中说明的现有列,那么也许它只是一个点,而您还没有清除缓存!?
应该没有必要解决这个问题,这应该可以在你的最终解决。悬停你可以尝试更改select / typemap以查看它是否有任何区别,你也应该尝试在整个控制流程中进行调试,以便了解可能出现问题的地方:
$query->getSelectTypeMap()->addDefaults(['visit_date' => 'integer']);
$query->getTypeMap()->addDefaults(['visit_date' => 'integer']);