Slim Framework with ORM (Eloquent) connect multiple db - orm

I am using Slim Framework with Eloquent 4.1.x as ORM for a project and need to connect to multiple databases.
I followed this link to setup the ORM. But how can I connect to multiple databases by following the given tutorial?
In my models I have different files that are used for different tables in different databases.
Like the User.php file has the following,
<?php
namespace Service\Framework\Model;
use Illuminate\Database\Eloquent\Model;
class Users extends Model {
protected $table = 'users';
}
This class uses the users table in db_2 database. I want to switch from the default database db_1 in a method in this Class. Like,
<?php
namespace Service\Framework\Model;
use Illuminate\Database\Eloquent\Model;
class Users extends Model {
protected $table = 'users';
public function getUsers() {
// Switch the database to db_2
$users = self::all();
// Again switch back to default database db_1
return $users;
}
}
How can I do that? Please help me with some suggestions.
P.S: I am not using Capsule here.
EDIT #1
So the code I am using to setup a single connection is as follows,
$settings = array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db_1',
'username' => 'dbuser',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
);
// Bootstrap Eloquent ORM
$container = new Container();
$connFactory = new ConnectionFactory($container);
$conn = $connFactory->make($settings);
$resolver = new ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
Model::setConnectionResolver($resolver);

You can connect to multiple databases with this simplified code:
$app = new \Slim\App([
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'products',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'db_second' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'second',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
]);
$container = $app->getContainer();
// connect to db with Illuminate larvel
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['db']);
$capsule->addConnection($container['settings']['db_second'], 'db_second');
$capsule->setAsGlobal();
$capsule->bootEloquent();
/// END connect to db
// to accsess the $capsule with our container from our controllers
$container['db'] = function($container) use ($capsule){
return $capsule;
};
and in the Model file put:
protected $connection = 'db_second';

First you should setup multiple connections. After connections are set up you can instruct model to use specific connection with $connection propery.
namespace Service\Framework\Model;
use Illuminate\Database\Eloquent\Model;
class Users extends Model {
protected $connection = 'mysql2';
protected $table = 'users';
}
In routes or controllers you can user setConnection() method.
$user = new User;
$user->setConnection('mysql2');
print_r($user->find(1));

Related

YII2 API Model Database Connection

I'm developing a API in YII2 with multiple databases. I want to choose the database in real time. The idea is to read one variable available in controler (API key) to identify the correct connection database in model.
For example in webapplication like a portal i have (it works):
in db default connection i have
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbcompany_0',
'username' => 'root',
'password' => 'XXXXXXXXXXXXXXXXXXXX',
'charset' => 'utf8',
],
In model I have
public function tableName()
{
$schema = '';
$user = User::find(Yii::app()->user->id);
$schema = "dbcompany_". $user->CompanyId;
return $schema . '.' . 'customer';
}
With this approach I just have a single db connection for all database companies.
How I can apply the same\similiar approach in API. I don't have sessions.
Any idea is well welcome.
Create a new component under db as:
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbcompany_0',
'username' => 'root',
'password' => 'XXXXXXXXXXXXXXXXXXXX',
'charset' => 'utf8',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbcompany_0',
'username' => 'root',
'password' => 'XXXXXXXXXXXXXXXXXXXX',
'charset' => 'utf8',
],
And use it as:
Yii::$app->db1
Yii::$app->db2;
All requests are authenticate using a token and this is associated to the user. When the authentication is done YII Framework assigns the userid to \Yii::$app->user->identity->id. So from anywhere I can identify the company owner in model.
public static function tableName(){
$moreinfo= account\Userextra::findOne(\Yii::$app->user->identity->id);
$schema = \Yii::$app->params['table.prefix'] . $moreinfo->CompanyId;
return $schema . '.country';
}

Cakephp - [Oracle] make query to another users table

I need que make a select query from a table outside my user. I can make the select query from toad for example but in cakephp i get this error:
Missing Database Table Error: Table cursos for model Curso was not
found in datasource ot.
I understand the error occurs because the table doesn't exists directly in my user.
The question is: is there any way to get the data in this situation??
look what I understand the problem you have 2 databases and users want to use one for work:
Multiple Database connection issue CakePHP
in your database.php You can configure 2 connections
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'test',
'password' => 'test1',
'database' => 'test_portal',
'prefix' => ''
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'dfffd_23',
'password' => 'dsfsd324',
'database' => 'testdbuser',
'prefix' => ''
//'encoding' => 'utf8',
);
}
now in your model you have to specify which of the settings or connections want used for so you can select the users that you want.
class Example extends AppModel {
public $useDbConfig = 'test';
}
you can see this in the documentation

Yii: override getDbConnection method and set CDbConnection from the master db to a slave db

For some reason I cant switch the db connection to save data in the secondary dbs.
db tables from the main db:
I have a website model class that stores data about a website.
WebsiteM extends Website wich extends CActiveRecord
I have a url model class that stores data about a url.
UrlM extends Url wich extends CActiveRecord
Each url belongs to a website; each url has a FK named website_id that tells the website that it belongs to;
Because the website model and url model are changing daily, I wrote for each model a new class that extends them, like websiteM extends website and urlM extends url and this way when I regenerate the models using gii, my enhancements are always saved.
db table from the slave db:
I have a slave url model class that stores data about a url in one of the slave active dbs.
The slave url model is the same as the main url model;
It's like this: UrlSlaveM extends UrlSlave wich extends HActiveRecord wich extends CActiveRecord
At some point, I try to switch to a secondary db, by using relations to find out the id of the db that I need to switch to, after new class instance like:
$model_urlSlave_new = new UrlSlaveM();
$model_urlSlave_new::$server_id = $model_url->websiteM_relation->database_id;
This should work, but it does not. This is where the action happens, this is where the connection should switch:
class HActiveRecord extends CActiveRecord {
public static $server_id = 2;
public static $master_db;
public function getDbConnection() {
self::$master_db = Yii::app()->{"db" . self::$server_id};
if (self::$master_db instanceof CDbConnection) {
self::$master_db->setActive(true);
return self::$master_db;
}
else
throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
}
}
settings in main.php
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
'db2' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => '',
'class' => 'CDbConnection' // DO NOT FORGET THIS!
),
'db1' => array(
'connectionString' => 'mysql:host=localhost;dbname=dvc1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => '',
'class' => 'CDbConnection' // DO NOT FORGET THIS!
),
I found the solution:
The following code was moved to the main model class and the hactiverecord class was deleted.
public static $server_id = 2;
public static $master_db;
public function getDbConnection() {
self::$master_db = Yii::app()->{"db" . self::$server_id};
if (self::$master_db instanceof CDbConnection) {
self::$master_db->setActive(true);
return self::$master_db;
}
else
throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
}
it works. tested.

SaaS application structure in YII using separate database

I need create a SaaS application structure in YII Framework using separate database, but the examples in the web are using a single database. Any information or handbook will be welcome.
Thanks
You can use a second DB by adding it to your main.php ex:
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=database',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'schemaCachingDuration' => 3600,
'enableProfiling' => true,
),
'db2'=>array(
'connectionString' => 'mysql:host=localhost;dbname=database2',
'class'=>'CDbConnection',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'schemaCachingDuration' => 3600,
'enableProfiling' => true,
),
Then you can set the getDbConnection() method inside the active records that are to use this second db. I would suggest making a parent class extending from CActiveRecord with this code and then extending that.
It should contain:
public static $db2;
public function getDbConnection()
{
if(self::$db2!==null)
return self::$db2;
else
{
self::$db2=Yii::app()->db2;
if(self::$db2 instanceof CDbConnection)
{
self::$db2->setActive(true);
return self::$db2;
}
else
throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
}
}

CakePHP 2, how to AuthComponent::login() with plain-text password?

I would like to implement CakePHP 2 website over existing database with plain-text password field.
This is my AppController
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'home'),
'authError' => 'You cannot view this page',
'authorize' => array('Controller'),
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array('username' => 'user_id', 'password' => 'user_password')
)
)
)
);
public function isAuthorized($user) {
return true;
}
function beforeFilter() {
$this->Auth->allow('home');
//$this->Auth->authenticate = $this->User;
parent::beforeFilter();
}
This is my UserController.
class UsersController extends AppController {
public $paginate = array(
'fields' => array('user_id', 'user_desc', 'user_password'),
'limit' => 25,
'order' => array(
'user_id' => 'asc'
)
);
function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Cannot Login');
}
}
}
}
This is my User model
class User extends AppModel {
public $name = 'User';
public $primaryKey = 'user_id';
public $belongsTo = 'Group';
}
According to those files above, when I pressed the Login button on login.ctp, I saw
select * from users where user_password = 'this_is_hashing_password'
on the sql dump section.
So, how to turn-off the automatic hashing algorithm, so the login() will compare the user input to the database stored password as plain-text???
I have tried lots of reading on the CakePHP book but I cannot find any, also using hashPasswords($data) technique which found from the internet is not working.
Please help.
Kongthap.
The best answer really is to batch-process your stored passwords so they are hashed, however there are cases where you may be adding a Cake app to an existing application that hashes passwords differently (say by not hashing them at all), so the question is valid even if the goal in this case is not.
Try these resources for modifying Cake's password hashing function, depending on your Cake version:
Cake 2.x
Cake 1.3
I got plain text password working in Cakephp 3, this should only be use for DEVELOPMENT purpose, you should never store password in plain text in production.
That being said, during development, plain text password allows me to focus on login instead of implementing a fully functional user encrypt/decrypt logic. Which is going to be replaced by an OAuth / SAML module anyway...
OK here comes the source code:
ROOT/src/Auth/PlainTextPasswordHasher.php
<?php
namespace App\Auth;
use Cake\Auth\AbstractPasswordHasher;
/**
* Plain text password for demo use, DO NOT PUSTH THIS TO PROD
*/
class PlainTextPasswordHasher extends AbstractPasswordHasher
{
public function hash($password)
{
return $password;
}
public function check($password, $hashedPassword)
{
return $password === $hashedPassword;
}
}
ROOT/src/Controller/PagesController.php
<?php
class PagesController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'username',
'password' => 'password',
],
'passwordHasher' => [
'className' => 'PlainText',
],
'userModel' => 'YourUsers',
]
],
'loginAction' => [
'controller' => 'Logins',
'action' => 'login'
]
]);
}
}
Source: This video https://www.youtube.com/watch?v=eASSNS1f3V4 and this section of the official doc: https://book.cakephp.org/3.0/en/controllers/components/authentication.html#creating-custom-password-hasher-classes