configuring database connection in Yii framework - php

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',

Related

Cannot connect to Google Cloud SQL through Google App Engine with Pdo_Mysql in ZF2

I have been trying to solve this issue already for a few days without any results. When using default php pdo object I can connect to the database:
$db = new \PDO('mysql:unix_socket=/cloudsql/project-id:database-instance;dbname=test',
'root', // username
'' // password
);
But when trying to connect with ZF2 adapter the connection just times out.
'db' => array(
'driver' => 'Pdo_Mysql',
'database' => 'test',
'username' => 'root',
'unix_socket' => '/cloudsql/project-id:database-instance',
),
I am quite sure that the problem is somehow with the unix_socket as I can connect to the Cloud SQL server from my localhost directly without socket:
'db' => array(
'driver' => 'Pdo_Mysql',
'host' => 'xxx.xxx.xxx.xxx',
'database' => 'test',
'username' => 'user',
'password' => 'password',
),
What I am missing?
unix_socket isn't a recognized option for the pdo driver (https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Adapter/Driver/Pdo/Connection.php#L164). Try this instead
'db' => array(
'dsn' => 'mysql:unix_socket=/cloudsql/project-id:database-instance;dbname=test',
'username' => 'user',
'password' => 'password',
)

Kohana 3.3 Database::instance('name') not working

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

using "CDbAuthManager" generates exception "CPhpAuthManager" is not defined

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

How do I set table prefix in Yii

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_',
),

Database mysql proxy error while connect with YII

I have configure mysql cluster with one master and one slave. Configured mysql proxy as common ip to access the servers. when i connect using mysql command its successfully connecting. But when i tried to access mysql proxy in yii application I am getting following error
2011/12/01 05:25:40 [error] [exception.CDbException] SQLSTATE[42000]
[1049] Unknown database 'PortalDB' 2011/12/01 05:25:40 [error]
[exception.CDbException] exception 'CDbException' with message
'CDbConnection failed to open the DB connection.' in
/var/www/YII/yii-1.1.8.r3324/framework/db/CDbConnection.php:370
Below is the db configuration in YII
'db'=>array(
'connectionString' => 'mysql:host=xxxx;dbname=PortalDB;port=4040;',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'mysql',
'charset' => 'utf8',
'queryCachingDuration' => 300,
'enableParamLogging' => true
),
'db2'=>array(
'class' => 'CDbConnection',
'connectionString' => 'mysql:host=xxxx;dbname=PortalDB;port=4040;',//devdbserver.c27hiz0xxzr5.us-east-1.rds.amazonaws.com
'emulatePrepare' => true,
'username' => 'root',
'password' => 'mysql',
'charset' => 'utf8',
'queryCachingDuration' => 300,
'enableParamLogging' => true
),

Categories