How do I use datasources in CakePHP 2?

允我心安 提交于 2019-12-12 19:49:47

问题


My MongoDB datasource is located in plugins/mongodb.

According to the new class loader in 2.0 I should do this:

App::uses('MongodbSource', 'Mongodb.Model/Datasource');

But how do I initiate it?

Or is it best practice to use the ConnectionManager? If so, how do I import it?


回答1:


If you WANT to use your way and loading this datasource "by hand" and not like Matt said, you would initiate it like this:

# /path/to/your/datasource
class MongoDbDatasource {...} //check how this class is named!

Within your file where you load it you can do this:

App::uses('MongodbSource', 'Mongodb.Model/Datasource');
$mongodb = new MongoDbDatasource();

But as said, the databsae configuration would be the better way:

public $default = array(
    'datasource' => 'Mongodb.MongodbSource',
    'database' => 'mydbname',
    'host' => 'yourhost',
    'port' => 'yourport',
    'login' => 'yourlogin',
    'password' => 'yourpassword'
);

Now you just have so add CakePlugin::load('Mongodb'); to your bootstrap.php so your plugin will be loaded.




回答2:


You need to tell your database configuration which datasource to use:

class DATABASE_CONFIG {

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'user',
    'password' => 'password',
    'database' => 'database_name',
    'prefix' => '',
);

}


来源:https://stackoverflow.com/questions/6408629/how-do-i-use-datasources-in-cakephp-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!