Problems using multiple databases
I have many different databases I want access to, so I have added several (3) pdo mysql connections to $phalcon = \phalcon\DI\FactoryDefault()
using $phalcon->set('dbname', \phalcon\db\adapter\pdo\mysql($info);
This is all good, I can use $phalcon->get('dbname'); to retrieve the connection again, its working like a charm, but when I use initialize on a class, its not really working:
public function initialize()
{
$this->setConnectionService('dbname');
}
This might be called, but its not used, if I don't set it in a model then it uses the standard which is 'db', that works as long as I have a 'db' set on my DI. If I sent something else, then I just can't make it work, its gives an error stateing that the table can't be found, and I can see why, its trying to get it from the wrong database, ie. its trying to select from table2, which is on test2 database, but its trying to get it from database test1, I get this error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test1.table2' doesn't exist'
Hopefully im just doing something wrong here, I tested for versions 5, 6, 7 and 8, same error as far as I can see, even tho it changes a bit in text and type.
Following code has been used to test:
<?php
class table1 extends \Phalcon\Mvc\Model
{
public function getSource()
{
return 'table1';
}
public function initialize()
{
echo('calling initialize on table1'."\n");
$this->setConnectionService('db');
}
}
class table2 extends \Phalcon\Mvc\Model
{
public function getSource()
{
return 'table2';
}
public function initialize()
{
echo('calling initialize on table2'."\n");
$this->setConnectionService('test2');
}
}
require_once('mysql_utf8.php');
$phalcon = new \Phalcon\DI\FactoryDefault();
global $MySQL;
$test1info = array(
'host' => $MySQL['hostname'],
'username' => $MySQL['username'],
'password' => $MySQL['password'],
'dbname' => 'test1',
'charset' => 'utf8',
);
$test1 = new \phalcon\db\adapter\pdo\mysql($test1info);
$phalcon->set('db', $test1);
$test2info = array(
'host' => $MySQL['hostname'],
'username' => $MySQL['username'],
'password' => $MySQL['password'],
'dbname' => 'test2',
'charset' => 'utf8',
);
$test2 = new \phalcon\db\adapter\pdo\mysql($test2info);
$phalcon->set('test2', $test2);
$table1 = table1::findFirst(array("data = 'table1_data1'"));
echo($table1->table1_id."\n");
$table1 = table1::findFirst(array("data = 'table1_data2'"));
echo($table1->table1_id."\n");
$table2 = table2::findFirst(array("data = 'table2_data1'"));
echo($table1->table2_id."\n");
$table2 = table2::findFirst(array("data = 'table2_data2'"));
echo($table1->table2_id."\n");
This is the mysql databases
-- CREATED ON DATABASE test1
CREATE TABLE IF NOT EXISTS `table1` (
`table1_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`data` text CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`table1_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Data dump for tabellen `table1`
--
INSERT INTO `table1` (`table1_id`, `data`) VALUES
(1, 'table1_data1'),
(2, 'table1_data2');
-- CREATED ON DATABASE test2
CREATE TABLE IF NOT EXISTS `table2` (
`table2_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`data` text CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`table2_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Data dump for tabellen `table2`
--
INSERT INTO `table2` (`table2_id`, `data`) VALUES
(1, 'table2_data1'),
(2, 'table2_data2');
Following is output from that script: ///////////////////////////// calling initialize on table1 1 2 calling initialize on table2
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test1.table2' doesn't exist' in phalconTest.php on line 67
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test1.table2' doesn't exist in phalconTest.php on line 67
Call Stack: 0.0006 738328 1. {main}() phalconTest.php:0 0.0156 1034136 2. Phalcon\Mvc\Model::findFirst()phalconTest.php:67 /////////////////////////////
Im I just doing it wrong here?
This is indeed a bug, :/, also I this #217 is caused by the same reason, I'll add a fix for this the next week, now I'm on vacation ;)
Hi gyde, this is now fixed in 0.8.0, can you test with this version please?
I will tests it later today or tomorrow, I have a planning meeting today, so not sure I will be able to do it today.
I added a unit-test for this in 0.8.0, thank you for reporting this
Dear Phalcon,
This issue isn't resolve? or is resolve now?
Thank
It should be resolved, do you have a case when this does not work?
@sjinks Yeah, in the forum i post one problem i have programming with multiple databases. The url is: http://forum.phalconphp.com/discussion/1275/problem-with-multiple-database-phalcon-version-1-2-4
Looks like this has been solved: http://forum.phalconphp.com/discussion/1275/problem-with-multiple-database-phalcon-version-1-2-4#C4556