Cakephp delete not working for multiple DB connect - cakephp

I am using cakephp 2.4 , am working on a project which uses two db connections based on the user logged in. while accessing the second DB and Retrieve data it is working fine, but when i delete record in the second DB table record its throw error as ..
Invalid catalog name: 1046 No database selected
Can anyone help on this ...
this is my DB connect code
$this->default['host'] = 'localhost';
$this->default['login'] = 'xxxxxx';
$this->default['password'] = 'xxxx';
$this->default['database'] = 'xxxxx';
$this->default['prefix'] = 'xxxx_';
if(!empty($_SESSION['subsites'])){
$this->test['host'] = 'localhost';
$this->test['login'] = 'xxxx';
$this->test['password'] = 'xxxxx';
$this->test['database'] = 'ccxxxx';
$this->test['prefix'] = 'xxxx'.'_';
}
then we used $useDBconfig ='test'; in model

Define multiple database configuration in app/config/database.php as
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'db1',
'prefix' => '',
//'encoding' => 'utf8',
);
public $subSites = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'db2',
'prefix' => '',
//'encoding' => 'utf8',
);
Now when you want to switch to subSites config just write following code:
$this->Model->useDbConfig = $subSites;

You can use $useDbConfig Attribute inside your Model.
class Example extends AppModel {
public $useDbConfig = 'user';
}
Inside your Controller, simply use:
$this->ModelName->useDbConfig = 'user';
for More Details, please click on http://book.cakephp.org/2.0/en/models/model-attributes.html

Related

Connect multiple database in one controller cakephp 2

Hello I am new to cakephp 2. I want to know, how to connect to database in one controller and loop for each. Please help me with details.
I have already set the following in the database config:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'db_one',
'prefix' => '',
//'encoding' => 'utf8',
);
public $database2 = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'db_announcement',
'prefix' => '',
//'encoding' => 'utf8',
);
You should research bit more in google before asking question here.
Anyways, CakePHP takes default database connection which is there in database.php in core folder, so all your queries which you execute in the database which is defined in default vairable in database.php and if you want to connect multiple database than you can try something like this.
In your database configuration file:
public $database2 = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '*******',
'database' => '*****',
'prefix' => '',
'encoding' => 'utf8',
);
And in your controller you can have connection in this way:
$db = ConnectionManager::getDataSource('user');
$list = $db->rawQuery('your query'); or customized CakePHP queries can work here too
I hope you will get your answer from this.
Thanks
$i=0;
$total=$list->fetchAll(PDO::FETCH_ASSOC);
instead of getDataSource method I would suggest to use $this->ModelName->setDataSource('mongo');
E.g If you want to fetch the data from User model of 'mongo' database source , which you have mentioned in database.php, then to set that it would look like.
$this->User->setDataSource('mongo')
in database.php you are having following code
public $mongo = array(
'datasource' => 'Mongodb.MongodbSource',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'mongo_data',
'prefix' => '',
'encoding' => 'utf8',
);

Cakephp How to change database connection

I have 2 controllers, ContentController for general user and ManageController for administrator. I need to change the connection from default to admin and I have this code in my database.php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => '',
'database' => 'ComputerScience',
'prefix' => '',
'encoding' => 'utf8',
);
public $admin = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'admin',
'password' => '',
'database' => 'ComputerScience',
'prefix' => '',
'encoding' => 'utf8',
);
}
Thank you
So, inside your Model, you would use the useDbConfig Attribute:
class Example extends AppModel {
public $useDbConfig = 'admin';
}
Inside your Controller, simply use:
$this->ModelName->useDbConfig = 'admin';
Thats all.
I would use Model::setDataSource() rather than just setting the database config var. This is because there are other possible changes that come with changing the datasource:
$this->Model->setDataSource('admin');

Yii : multiple databases connection fails

i read the yii docs and the following code should work;
well, it doesnt. :))
db is the primary database
db1 and db2 are the secondary databases
what is wrong here ?
the website is online, at www.linkbook.co, and it cant connect to any of the databases
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=linkbookco',
'emulatePrepare' => true,
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'tablePrefix' => '',
),
'db1' => array(
'connectionString' => 'mysql:host=localhost;dbname=linkbookco1',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'tablePrefix' => '',
'class' => 'CDbConnection' // DO NOT FORGET THIS!
),
'db2' => array(
'connectionString' => 'mysql:host=localhost;dbname=linkbookco2',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'tablePrefix' => '',
'class' => 'CDbConnection' // DO NOT FORGET THIS!
),
db is the predefined component of Yii, so bedefault CActiveRecord makes connection using db. So for other components you have created using CDbConnection class you have to activate connection for them externally.
SO You need to overwrite getDbConnection() method of CActiveRecord.
Extend the CActiveRecord for specific database connection like db1.
Save this as Db1CActiveRecord.php and place in components directory.
<?php
/**
*
* Used for db1 database connection
*
*/
class Db1CActiveRecord extends CActiveRecord {
private static $db1 = null;
public function getDbConnection()
{
if (self::$db1 !== null)
return self::$db1;
else
{
self::$db1 = Yii::app()->db1;
if (self::$db1 instanceof CDbConnection)
{
self::$db1->setActive(true);
return self::$db1;
}
else
throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
}
}
}
Now you need to use Db1CActiveRecord for the model class of database db1.
Like:
class Db1Model extends Db1CActiveRecord{
......
}
Implement this way for both db1 & db2 database.

Variable database setting Cakephp

I'm searching a way to create a variable database in Cakephp.
I'm creating a website in wich the database setting (user, pass, db) depends of the user who logg's in. The database settings come from another database.
Thanks in advance,
AƤron
-- ANSWER
Thanks for your quick reply.
I know how to switch between databases, but the database settings have to be variable.
Example
$db = $this->DB_SET['DB_SET']['db']; //from model DB_SET the database
$db_login= $this->DB_SET['DB_SET']['login'];
$db_host= $this->DB_SET['DB_SET']['host'];
$db_pass= $this->DB_SET['DB_SET']['pass'];
var $db2 = array(
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_login,
'password' => $db_pass,
'database' => $db,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
);
If you're looking to create database connections on the fly, you may want to use ConnectionManager::create().
// get logged in user's id and create a connection for them
$id = $this->Auth->user('id');
ConnectionManager::create("user_$id_db", array(
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_login,
'password' => $db_pass,
'database' => $db,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
));
Then set your models to use it when you're ready.
// get logged in user's id and use their connection
$id = $this->Auth->user('id');
$this->Model->setDataSource("user_$id_db");

How I will access a table from another Databse in cakephp

I have two database in my project.
I have declared two connection variable in database.php. As follows:
var $development = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'xxxx',
'login' => 'xxxx',
'password' => 'xxxx',
'database' => 'yyyy',
'prefix' => '',
);
var $production = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'xxxxxx',
'login' => 'xx',
'password' => 'xx',
'database' => 'xxx',
'prefix' => '',
);
Now I am using the development as the default connection.
In one controller function I need to fetch some values from the anther DB. How can I get the other DB data there?
If any body can help regarding this I will very very obiliged to him/her.
Thank you in advance .
you can use $useDbConfig in your model class to define which database should it use for data source
class Example extends AppModel {
var $useDbConfig = 'development';
}
class Example extends AppModel {
var $useDbConfig = 'production';
}
and you can check the detail usage in cakephp document
http://book.cakephp.org/view/1057/Model-Attributes

Resources