English 中文(简体)
How do I use datasources in CakePHP 2?
原标题:

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?

最佳回答

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.

问题回答

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  =>   ,
);

}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签