官方文檔上面有這樣的描述: Please note that if you are using the Mongo driver provided by PHP 7, the ODM will not work for you. There is an incubator adapter but all the Mongo code must be rewritten (new Bson type instead of arrays, no MongoId, no MongoDate, etc…). Please ensure that you test your code before upgrading to PHP 7 and/or Phalcon 3+ 大概意思就是若是你用的php7 提供的mongo驅動的話是不行的。 緣由是: phalcon提供的驅動是基於mongo的驅動, 可是php7 已經不支持了, php7使用的是mongodb的驅動。php
不過官方提供瞭解決方案,https://github.com/phalcon/incubator 按照文檔上面的去用conposer安裝。git
use Phalcon\Mvc\Collection\Manager; use Phalcon\Db\Adapter\MongoDB\Client; // Initialise the mongo DB connection. $di->setShared('mongo', function () { /** @var \Phalcon\DiInterface $this */ $config = $this->getShared('config'); if (!$config->database->mongo->username || !$config->database->mongo->password) { $dsn = 'mongodb://' . $config->database->mongo->host; } else { $dsn = sprintf( 'mongodb://%s:%s@%s', $config->database->mongo->username, $config->database->mongo->password, $config->database->mongo->host ); } $mongo = new Client($dsn); return $mongo->selectDatabase($config->database->mongo->dbname); }); // Collection Manager is required for MongoDB $di->setShared('collectionManager', function () { return new Manager(); });
這樣配置好之後, 發現報錯, 找不到 Client
類,這個安裝完成後是安裝到了 composer
的目錄 vender
目錄下面, 那麼你須要在執行代碼以前使用 composer
的 autoload
, 這樣在找不到phalcon
擴展的狀況下會自動加載 composer
的文件, 如此就沒有問題了。github
use Phalcon\Mvc\MongoCollection; class UserCollection extends MongoCollection { public $name; public $email; public $password; public function getSource() { return 'users'; } }
如此便可使用phalcon
的collection
組件。mongodb