In a project I am working on I need to set a table prefix for the project which I can change later. Browsing through the docs I came across this :
http://www.yiiframework.com/doc/api/1.1/CDbConnection#tablePrefix-detail
But it is not explained where I implement this. I mean should I put it in protected/config/main.php or edit the core files ?
You put it in the config file, along with other db configuration, like this:
'db'=>array(
'connectionString' => 'xxxxx',
'username' => 'xxxxx',
'password' => 'xxxxx',
'tablePrefix' => 'tbl_',
),
All public properties of any component can be set in the config file this way.
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=###',
'emulatePrepare' => true,
'username' => '###',
'password' => '###',
'charset' => '###',
'tablePrefix' => 'r_',
),
Related
I have an issue with Kohana 3.3 and using different database configurations.
I have a config/database.php with 'default' config and 'other' like this:
return array
(
'default' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'database-one',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
'other' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'database-two',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
));
But in a Controller or Model when trying to use:
Database::instance('other');
Kohana will still use the 'default' configuration. What am I doing wrong?
Thanks!
If you would like to change currently used connection by kohana try this:
Database::$default = 'other';
From this line your code will use 'other' connection till you will switch it again to 'default' using same way.
You can also use another DB configuration once when executing the query in simple way:
DB::...->execute('other');
Or if you store your DB instance earlier:
$other = Database::instance('other');
DB::...->execute($other);
By ... I mean your query.
You need to store the connection in a variable, or the default connection will be used.
Documentation
I have created an app with laravel and I have a set of environments I want to run on the site as always. The setup comes from the start.php file where I declare the environments like so:
$env = $app->detectEnvironment(array(
'local' => array('Mark-macbook.local'),
'development' => array('excelsior.servers.prgn.misp.co.uk'),
'production' => array('excelsior.servers.prgn.misp.co.uk'),
));
I then create files in the root where server.php is and create files that have the correct database details for each environment in like so:
.env.local.php
<?php
return array(
'DATABASE_HOST' => 'localhost',
'DATABASE_NAME' => 'borough',
'DATABASE_USER' => 'root',
'DATABASE_PASSWORD' => 'root',
'UNIX_SOCKET' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);
.env.development.php
<?php
return array(
'DATABASE_HOST' => 'localhost',
'DATABASE_NAME' => 'db-name',
'DATABASE_USER' => 'db-user',
'DATABASE_PASSWORD' => 'pass'
);
.env.production.php
etc etc
Then in my database.php file in app/config I have this setup:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['DATABASE_HOST'],
'unix_socket' => $_ENV['UNIX_SOCKET'],
'database' => $_ENV['DATABASE_NAME'],
'username' => $_ENV['DATABASE_USER'],
'password' => $_ENV['DATABASE_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
So this is all setup as how I know it usually works but when I run php artisan serve I get this error:
{"error":{"type":"ErrorException","message":"Undefined index: DATABASE_HOST","file":"\/Freelance\/Current Projects\/borough\/build\/borough-cc\/app\/config\/database.php","line":67}}
Does anyone know why this would happen and what I may be doing wrong here?
Cheers
From memory, as I haven't used Laravel a lot, you are not using it correctly.
Basically, the database access will look at the configuration keys in a kind of global configuration. This global config is the result of the combination of your config files from the config folder and your environment config.
Therefore, you just need to redeclare (or simply declare) your environment specific variables in the environment config.
.env.local.php
<?php
return array(
'connections' => array(
'mysql' => array(
'host' => 'localhost',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'borough,
'username' => 'root,
'password' => 'root',
),
),
),
);
database.php
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
I'm a bit afraid in your example your are not defining the same keys in both config... Basically, you define the common config first, then you redefine whatever you need in the environment specific files.
I'm digging into ZF2, and I've run into some confusion on how to use Zend\Config with Zend\Db to manually set up a connection.
In different places in the manual, there are db configs in different formats.
This one shows a flat array as the config format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => 'zend_db_example',
'username' => 'developer',
'password' => 'developer-password'
));
While this one shows a nested format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.config.introduction.html
$configArray = array(
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
What I expect to happen is that I can call for a new db adapter like so, but this throws exceptions:
$config = new Zend\Config\Config(
array(
'db' => array(
'adapter' => 'Mysqli',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db);
What I end up having to do is:
$config = new Zend\Config\Config(
array(
'db' => array(
'driver' => 'Mysqli',
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'database' => 'mydatabase'
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db->toArray());
Is there a better way of achieving what I'm trying to achieve without having to resort to the service manager?
Ignore the example from the Zend Config introduction page, that's just showing how to make a config object from a PHP array, the structure of the array isn't meant to show anything in particular.
Since you don't want to use the service manager, you need to pass the parameters to the adapter class in the structure it expects. It expects an array, a config object won't work. You've worked out what the structure of the array is, so that's what you need to use.
I think this page in the docs: http://framework.zend.com/manual/2.3/en/tutorials/tutorial.dbadapter.html (the "Basic setup" section) gives a better explanation of the service manager approach, which is how I'd do it in an MVC app at least.
Hi I am tring to implement RBAC to an application . I set auth manager to CDbAuthManager:
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
'authManager'=>array(
'class'>'CDbAuthManager',
'connectionID' => 'db',
),
and when I use
$auth = Yii::app()->authManager;
it generates " Property "CPhpAuthManager.0" is not defined. "
I checked db connection and i am able to do crud . what am I doing wrong ?
I found that tables names was case sensitive. modified tables names .. all worked
In the main.php file of the Yii framework, there are some configuration options. This is how it sets up mysql
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
),
On my MAMP system, I have to specify the port as 8889. How would I add it into this?
thanks
I added the port like this and it seems to work
'db'=>array(
'connectionString' => 'mysql:host=localhost;port=8889;dbname=testdrive',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
),
can you not add it here to your connectionString
'connectionString' => 'mysql:host=localhost;dbname=testdrive;port=8889',