http://docs.phalconphp.com/en/0.6.0/reference/odm.html
设置多个数据库¶
在Phalcon中,所有模型都可以属于同一个数据库连接或具有单独的数据库连接。实际上,当Phalcon \ Mvc \ Collection需要连接到数据库时,它会在应用程序的服务容器中请求“mongo”服务。您可以在initialize方法中覆盖此服务设置:
<?php
//This service returns a mongo database at 192.168.1.100
$di->set('mongo1', function() {
$mongo = new Mongo("mongodb://scott:nekhen@192.168.1.100");
return $mongo->selectDb("management");
});
//This service returns a mongo database at localhost
$di->set('mongo2', function() {
$mongo = new Mongo("mongodb://localhost");
return $mongo->selectDb("invoicing");
});
然后,在Initialize方法中,我们为模型定义连接服务:
<?php
class Robots extends \Phalcon\Mvc\Collection
{
public function initialize()
{
$this->setConnectionService('management'); // here?
}
}
答案 0 :(得分:0)
你是对的。文件错了。正确的用法是使用setConnectionService
设置适当的服务名称(而不是集合)。
http://docs.phalconphp.com/en/latest/reference/odm.html
<?php
// This service returns a mongo database at 192.168.1.100
$di->set(
'mongo1',
function()
{
$mongo = new Mongo("mongodb://scott:nekhen@192.168.1.100");
return $mongo->selectDb("management");
}
);
// This service returns a mongo database at localhost
$di->set(
'mongo2',
function()
{
$mongo = new Mongo("mongodb://localhost");
return $mongo->selectDb("invoicing");
}
);
然后,在Initialize方法中,我们为模型定义连接服务:
.. code-block :: php
<?php
class Robots extends \Phalcon\Mvc\Collection
{
public function initialize()
{
$this->setConnectionService('mongo1');
}
}