CodeIgniter 2 + Doctrine 2 with multiple database connections - database

I was wondering if is possible to have CodeIgniter 2 with Doctrine 2 with multiple database connections.
I currently got CodeIgniter 2 and Doctrine 2 to work with 1 database, but is it possible to do multiple database?
If so, how can this be accomplished?

I am unsure if you can do this where the two databases interact directly. (Cross-Database Joins/etc..)
However in your load-up for doctrine you probably have something along the lines of this:
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'user' => $db['default']['username'],
'password' => $db['default']['password'],
'host' => $db['default']['hostname'],
'dbname' => $db['default']['database']
);
// Create EntityManager
$this->em = EntityManager::create($connectionOptions, $doctrine_config);
When You would just configure a second entity manager with the other db, perhaps with a seperate doctrine-config:
// Database connection information
$connectionOptions2 = array(
'driver' => 'pdo_mysql',
'user' => $db['other']['username'],
'password' => $db['other']['password'],
'host' => $db['other']['hostname'],
'dbname' => $db['other']['database']
);
// Create EntityManager
$this->emOther = EntityManager::create($connectionOptions2, $doctrine_config2);

Related

Existing database integration with Laravel 5.0

I am new to laravel. I've got the whole database designed in phpmyadmin. Now I want to integrate the existing database in Laravel. Is there any way to do that? If so then do I have to create models?
Yes, you can use your database in laravel but at first you have to provide your database credentials to allow the framework/Laravel to access your database. So, you can use a .env file or simply you can use the config/database.php to provide credentials, for example, to use your mySql database all you need to setup the database configuration as follows:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'your_database_name'),
'username' => env('DB_USERNAME', 'your_database_username'),
'password' => env('DB_PASSWORD', 'your_database_password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
]
Then regarding your second question, yes, you have to create models for each table or you can use DB facade to run queries directly from your controllers (not recommended). For example, to access your posts table you can create a Post model in app folder which may look something like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
// If your table name is other than posts
// then use this property to tell Laravel
// the table name.
protected $table = 'posts';
}
Then you can use something like this:
$posts = \App\Post::all(); // Get all posts
$post = \App\Post::find(1); // Get the post with id 1
If you don't want to use Eloquent Model like one I've created above then you may use something like this:
$posts = \DB::table('posts')->get(); // Get allp posts
These are some basic examples to get you start. To learn more about the use cases of models/database you should visit Laravel website and read the manual (find Database section and check Query Builder and Eloquent).

Laravel 5.0 switching from default database to another to use on all views and controllers

well I have a problem when it comes to switch databases.
I have a database "A" set up as default on the config.database file; besides that I have in connections array a database "B".
'default' => "A",
....
'connections' => [
'A' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
],
'B' => [
'driver' => 'mysql',
'host' => env('DB_HOST_1', 'localhost'),
'database' => env('DB_DATABASE_1', 'forge'),
'username' => env('DB_USERNAME_1', 'forge'),
'password' => env('DB_PASSWORD_1', ''),
],
.....
when I log in and go to POST login method in controller, I switched to database "B"
\Config::set('database.default', "B");
to use the 'User' table and get credentials to log in.
if(\Auth::attempt(array('username'=>$username , 'password'=>$password) ) )...
which it works fine. HOWEVER, when I go to a different blade.php
I will get the credentials from the "USER" table located on default connection ("A") instead of USER table located in B
So when I do
echo Auth::user()->firstName()." - ".Auth::user()->id;
//it will display Carlos - 10 instead of displaying Luis - 10
So I guess it is looking for the row where id=10 but on table A(dafault connection) instead of B (one I switched using Config::set()...
I have tried also putting Config::set('database.default', "B") on each view, but it still gives me the credentials from table A
any help?
how can i change the database permanently?
by the way, why do I have to call 2 databases for user credentials? well because on my login form I have an input to enter company name (A or B).
Try changing the connection like this:
DB::setDefaultConnection('A');

Zend 2 db transactions?

How do we use transactions in Zend 2? I didn't find anything in the API, and a couple questions for Zend 1 refered to the regular PDO functions, but I don't see anything like that in Zend 2.
The documentation is lacking a bit in this department for ZF2:
Start Transaction:
$this->adapter->getDriver()->getConnection()->beginTransaction();
Commit Transaction:
$this->adapter->getDriver()->getConnection()->commit();
Rollback Transaction:
$this->adapter->getDriver()->getConnection()->rollback();
Try this:
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=db;hostname=localhost',
'username' => 'root',
'password' => 'password',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
));
$adapter->getDriver()->getConnection()->beginTransaction();
DB will run command:
START TRANSACTION

Multiple applications can share the same ACL tables?

Currently I have to share my database with many CakePHP applications and the tables are prefixed to identify each application respectively.
So, can many applications share the same ACL tables?
Or, I could change the default names of the tables ACL and add the prefix of each application, eg. app_aros, app_acos, app_aros_acos?
/* beforeFilter() # AppController */
$this->Acl->Aro->useTable = 'app_aros';
$this->Acl->Aco->useTable = 'app_acos';
This code worked but I haven't found a way to change the tablename of the model Permission...
Suggestions? What could I do?
You have change this line in app/Config/core.php in all of your apps
Configure::write('Acl.database', 'default');
to:
Configure::write('Acl.database', 'your_acl_connection');
And also add connection in app/Config/database.php
example:
public $your_acl_connection = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
Simply you will have one DB for managing all yours app ACLs.
In this DB you create all ACL tables.

How can I connect to second database with my cakephp application

my application's requirement is that to display data of magento database table forexample admin_user that is reside at locally connected pc.
so I need to keep as it is my cakephp database values in displaying-modifying-etc, and in only one page magento's database value needs to be printed and updated.
I have kept 2 variables in databse.php
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'myappdatabase',
'prefix' => '',
//'encoding' => 'utf8',
);
var $vsdatabase = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '192.168.1.36',
'login' => 'root',
'password' => '',
'database' => 'magento',
'prefix' => '',
);
and in controller,
App::import('Model','ConnectionManager');
$db = ConnectionManager::getDataSource('vsdatabase');
$database = $db->config['database'];
$data = $this->User->query("select * from $database.admin_user as t1");
the host I like to keep as written above means default is from my local database and other is remote PC's magento database
If I both host keep same then its working but if write different then It's not working
So I solve this problem?
Plz help me in finding out solution
What you do is plain wrong and even if you would use that code it should belong into a model not a controller.
Simply create a new model named after the magento table, but I would prefix it with magento or something. The model has to be configured to use this db connection.
class MagentoUser extends AppModel {
public $useDbConfig = 'vsdatabase';
}
You can also init models with other aliases and data sources on the fly using ClassRegistry. See http://api20.cakephp.org/class/class-registry#method-ClassRegistryinit
And stay away from using plain SQL queries, you will very likely ending up writing insecure queries and lose other features of the CakePHP ORM.

Resources