致命错误:使用 mongodb php 驱动程序 1.1.2 和 PHP 7.0.2 - Laravel 5.1 时找不到“MongoDate”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34667609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Class 'MongoDate' not found when using mongodb php driver 1.1.2 and PHP 7.0.2 - Laravel 5.1
提问by Graham S.
I am trying to configure MongoDB to work with my Laravel 5.1 Homestead instance on a virtual Ubuntu 14.04 machine. I was able to successfully install the latest version of MongoDB which supports PHP 7.0 using sudo pecl install mongodb
(this is correct for 7.0, notsudo pecl install mongo
anymore).
我正在尝试将 MongoDB 配置为在虚拟 Ubuntu 14.04 机器上使用我的 Laravel 5.1 Homestead 实例。我能够成功安装最新版本的MongoDB使用支持PHP 7.0 sudo pecl install mongodb
(这是正确的7.0,没有sudo pecl install mongo
了)。
I then added the extension in my php.ini files (all three) on my Ubuntu machine, each in:
然后我在我的 Ubuntu 机器上的 php.ini 文件(所有三个)中添加了扩展名,每个文件都在:
/etc/php/7.0/cli/php.ini
/etc/php/7.0/fpm/php.ini
/etc/php/7.0/cgi/php.ini
/etc/php/7.0/cli/php.ini
/etc/php/7.0/fpm/php.ini
/etc/php/7.0/cgi/php.ini
This is the extension I wrote which is correct for use with PHP 7.0:
这是我编写的适用于 PHP 7.0 的扩展:
extension=mongodb.so
(not mongo.so anymore)
extension=mongodb.so
(不再是 mongo.so)
When I run phpinfo()
in my browser, it states that MongoDB is properly configured with my PHP 7.0.
当我phpinfo()
在浏览器中运行时,它指出 MongoDB 已使用我的 PHP 7.0 正确配置。
If MongoDB is properly configured, how come I keep getting:
如果 MongoDB 配置正确,我怎么会不断收到:
Fatal error: Class 'MongoDate' not found
when I try to run my migrations and seeds with php artisan migrate:refresh --seed
?
当我尝试使用php artisan migrate:refresh --seed
?
I already tried:
我已经尝试过:
- rebooting the Ubuntu machine with
vagrant reload
andvagrant reload --provision
- Restarting PHP and Nginx with
sudo service nginx restart
andsudo service php7.0-fpm restart
- 使用
vagrant reload
和重新启动 Ubuntu 机器vagrant reload --provision
- 使用
sudo service nginx restart
和重新启动 PHP 和 Nginxsudo service php7.0-fpm restart
Neither have worked.
两者都没有工作。
回答by morrislaptop
As you mentioned you're using the new Mongo extension for PHP 7.
正如您所提到的,您正在使用 PHP 7 的新 Mongo 扩展。
The class names have changed from the older version, i.e.
类名已从旧版本更改,即
MongoClient
is now MongoDB\Driver\Manager
MongoClient
就是现在 MongoDB\Driver\Manager
MongoDate
is now MongoDB\BSON\UTCDateTime
MongoDate
就是现在 MongoDB\BSON\UTCDateTime
I'm not sure how backwards compatible everything is, but this should get you started!
我不确定所有东西的向后兼容程度,但这应该会让你开始!
回答by Jon M
Throughout our app we were regularly converting unix timestamps into MongoDate instances. Example:
在我们的应用程序中,我们定期将 Unix 时间戳转换为 MongoDate 实例。例子:
new MongoDate(strtotime('-1 day'));
I've therefore created a class to allow conversion between unix timestamps and the new MongoDB\BSON\UTCDateTime
, and back
因此,我创建了一个类来允许在 unix 时间戳和 new 之间进行转换MongoDB\BSON\UTCDateTime
,然后返回
<?php
class MongoHelper {
const SECONDS_IN_A_MILLISECOND = 1000;
public static function getMongoUTCDateTimeFromUnixTimestamp($timestamp) {
return new \MongoDB\BSON\UTCDateTime(intval($timestamp * self::SECONDS_IN_A_MILLISECOND));
}
public static function getUnixTimestampFromMongoUTCDateTime(\MongoDB\BSON\UTCDateTime $utc_date_time) {
return intval((string) $utc_date_time / self::SECONDS_IN_A_MILLISECOND);
}
}
Example usage:
用法示例:
MongoHelper::getMongoUTCDateTimeFromUnixTimestamp(strtotime('-1 day'));