I have yii application,, and i want to change the database connection..
first, my app is connect to 'trackstar' database, and later i want to change to 'taskmanagement' database..
So i just simply change the dbname in my code :
<?php
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Console Application',
// application components
'components'=>array(
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=taskmanagement',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
'authManager'=>array(
'class'=>'CDbAuthManager',
'connectionID'=>'db',
'itemTable' => 'tbl_auth_item',
'itemChildTable' => 'tbl_auth_item_child',
'assignmentTable' => 'tbl_auth_assignment',
),
),
);
but when i run the app i got error :
CDbCommand failed to execute the SQL statement: SQLSTATE[42S02]: Base
table or view not found: 1146 Table 'trackstar.tbl_auth_assignment'
doesn't exist. The SQL statement executed was: SELECT * FROM
tbl_auth_assignment
WHERE userid=:userid
the thing i dont understand is that why is still connect to trackstar database eventhough i just change the dbname to taskmanagement
thx before :)
I guess one your model queries written as straight SQL query.
EX:
SELECT * FROM databaseName.tableName
So, Even you change the database name in the config file these kinds of queries may not work. Please check your code in this way.
You are only configure the console.php Configuration . But Yii Web application use the main.php
Check your main.php file
its located on Your App folder -> Protected -> config ->main.php
Change Db connection on this file
You can use below code to update database dynamically
if ($city == 'ABC') {
$databse = 'db_abc';
} elseif ($city == 'BCD') {
$databse = 'db_bcd';
} else {
$databse = 'default_db';
}
$db = Yii::$app->getDb();
$db->dsn = "mysql:host=localhost;dbname=$databse";
Hope this help to override database connection string
Related
I am building the admission portal in laravel,
I have super admin database which has a schools table with 100 rows,
schools table structure
1.id
2.school_name
3.database details
I want to connect to the school database with its database details by its id.
technically
1.I will pass the school id from url
2.it will select that row from school table
3.after selecting the database details of particular school
4.will connect to the school database for further use.
I went through https://laracasts.com/discuss/channels/tips/set-up-dynamic-database-connection-globally
http://fideloper.com/laravel-multiple-database-connections
but no luck
please help to sort it out.
Actually you dont want multiple connections, but rather change existing connection.
public function setSchoolConnection($id) {
$school = School::find($id);
if ( $school ) {
config(['database.mysql' => [
'database' => $school->database,
'username' => $school->username,
'password' => $school->password
]]);
}
}
Now the default connection has been changed. I think.
If you don't want to change existing connection, just create a new connectio
config(['database.school' => [
'driver' => 'mysql',
'database' => $school->database,
'username' => $school->username,
'password' => $school->password
]]);
and use it like this
$users = DB::connection('school')->select(...);
Have you tried setting the connection in the School model? Laravel will take care of the rest, even if you have relations with different connection.
In your School model, I will override the constructor like so:
public function __construct(array $attributes = [])
{
$this->setConnection(env('DB_SCHOOL_CONNECTION'));
parent::__construct($attributes);
}
The one thing to be careful of is if you have a relation with a different connection, using whereHas or has in your query/builder wont work. as laravel will not be able to set the connection in the query generated.
I have following Data:
Database name = ThisDatabase
Table name = InfoData
cakePHP3 convert the names in
ThisDatabase = this_database
InfoData = info_data
My problem is that I have no chance to rename the DB or Table names so I have to disable or bypass the name converting in cakePHP3.
But I have no clue how I can do this.
How can i disable the converting? So I can use the actual names (ThisDatabase and InfoData).
You should follow naming convention for your files and cakephp to work properly, and specify the table name in initialize function inside your App\Model\Table\ArticlesTable.php
Here you can find related documentation
That's easy, CakePHP offers you a way to change the name of the table as you wish.
For the database, in the app config, set the database name :
...
'Datasources' => [
'default' => [
...
'username' => 'username',
'password' => 'password',
'database' => 'ThisDatabase', // Here you can set the database name
'encoding' => 'utf8',
..
]
],
...
Here is some doc
For the table name : In your table definition, change the table name
class Infodata extends Table
{
public function initialize(array $config)
{
$this->table('InfoData');
}
}
Here is is the doc
I am fairly new to using laravel framework. I have the following requirement. I have a domain - example.com, and its entire code stack is running in laravel. Lets say in the config default database connection is - 'db1'
Now, if the url becomes - example.com/country - I want the default database connection to become - 'db2'
And also I want the base URL to be changed to example.com/country, since all the modules are going to be the same. Only the database connection is going to change here.
Could someone help me out here?
I would do it the following way:
Put the list of allowed countries into config file (countries.php) .
In routes.php:
// choosing country
$country = '';
if (in_array(Request::segment(1), Config::get('countries'))) {
$country = Request::segment(1);
}
// making route for top level
if ($country != '') {
Route::any( '/', 'MainPage#index');
}
// making routes optionally prefixed by country
Route::group(
array('prefix' => $country,
function () {
// here all routes
});
In database.php where you have defined your connection you could add another connections for example:
'germany' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'germany_connection',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Now in the same file (although you should probably move it somewhere else) you can do:
if ($country == 'germany') {
DB::disconnect();
Config::set('database.default','germany');
DB::reconnect();
}
You can of course add many conditions here or if you have defined connection for each allowed country you can simply do:
if ($country != '' ) {
DB::disconnect();
Config::set('database.default', $country);
DB::reconnect();
}
It should work however I haven't tested it
I've made something similar using environments.
Add a new rule when detecting the environment to parse the URL and change it to the new environment. Then you can add a directory for the new environment where you set up the new database, base URL, etc.
I would like to connect to a second database with Yii at runtime. The database name would come from a database table after the user to login.
I saw in a tutorial I should do this:
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
));
Yii::app()->setComponent('db2',$db2);
But in my controler when I access Yii::app()->db2 get the error:
Property "CWebApplication.db2" is not defined
What am I doing wrong?
The following works for me:
Yii::app()->mongodb->setActive(false);
Yii::app()->mongodb->setServer('mongodb://localhost:27017');
Yii::app()->mongodb->setDb('db1');
Yii::app()->mongodb->setActive(true);
UPDATED: Try, instead instance, pass configurations:
Yii::app()->setComponent( 'db2', array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
)
);
Or, you may create special index on params in configurations, such as:
...
'params' => array(
'db2' => null,
),
And the use Yii::app()->params['db2'] = $db2
From this comment:
My problem is not with the creation of the component. Soon after
creating if I access Yii::app()->db2 its works, but when I try to
access via another model or controller I get the error
I think you are setting this component only once somewhere, and then making subsequent requests to different controllers.
You need to put the code, somewhere it is being called EVERYTIME, on every Request. thats how PHP works, there is no "global application state"
by default Yii comes with protected/components/controller.php has base controller for the rest of the app.
my suggestion would be to put your code on the init() method of that controller, so that it always gets called.
You mentioned the database name comes from a table once the user logs in, so you need to save that value in the session, in other to be able to access it in the other requests:
<?php
// After login in
Yii::app()->user->setState('db_name', $db_name);
// in protected/components/controller.php
public function init()
{
if (!Yii::app()->user->isGuest) {
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => Yii::app()->user->getState('db_name')
));
Yii::app()->setComponent('db2',$db2);
}
}
Hope it helps, I am assuming many things here :)
I'm trying to run a full text search against some data that is stored in mongoDb using Lithium.
Here is how I am trying to do it in my controller:
$mongodb = Connections::get('default')->connection;
$results = Page::connection()->connection->command(array("text" => "Page", 'search' => "term" ));
I've also tried:
$results = Page::connection()->connection->command(array("text" => "Page", 'search' => "term" ));
However, both of these return: Fatal error: Call to a member function command() on a non-object
What am I doing wrong?
EDIT:
I should add that a simple query on Page is working just fine. For instance:
$results = Page::find('all');
Does return an array with all of the documents in the pages collection like I would expect it to.
UPDATE 2:
I was running all of this from WAMP server. I tried today running it from a linux server, but still got the same exact error. I am really stumped by this and could use some help. Anyone have any ideas?
here is the Page model as it sits now:
<?php
namespace app\models;
use lithium\data\Connections; //added during debugging
use lithium\data\source\MongoDb; //added during debuging
class Page extends \lithium\data\Model {
}
?>
Here is my connection:
Connections::add('default', array(
'type' => 'MongoDb',
'host' => '192.168.48.128',
'database' => 'my_collection'
));
I'm doing it this way:
$plugins = Plugins::connection()->connection->command([
'text' => 'plugins',
'search' => $this->request->query['q']
]);
return compact('plugins');
so I'd recommend checking your configuration - is your model returning other data normally? Is the connection configuration correct?
Got help to figure it out... posting here for others to reference.
proper way of calling it is:
$conn = Model::connection();
$db = $conn->selectDB('db');
$result = $db->command(array(...
Works perfectly when done this way.