Tutorial using Mysql

I am looking for Yii tutorial using Mysql. I have downloaded Blog tutorial, which is using sqlite. I tried to use that tutorial with mysql, but getting errors.

Please provide Yii+Mysql tutorials, since Mysql is the standard of most php application.

Thanks!

Well, I think that one of the things with Yii is that it uses PHP-PDO wich makes it indepentent of datatabase. What errors do you get?

The database support in Yii is built on top of PDO, so you can easily switch to different types of DBMS (e.g. MySQL, PostgreSQL) without the need to change the code of your application. In order to use MySQL, you have to configure it in the application configuration (normally in protected/config/main.php) first. Add or modify it, finally it should contain something like that:



array(


    ......


    'components'=>array(


        ......


        'db'=>array(


            'class'=>'CDbConnection',


            'connectionString'=>'mysql:host=localhost;dbname=testdb',


            'username'=>'xyz',


            'password'=>'xxx',


            'charset'=>'UTF8',


        ),


    ),


)

Be sure to set the values for host, dbname, username and password correctly. Yii should now use MySQL for database operations. So you have to fill the database with the tables and data needed for the blog tutorial. You can use the SQL statements in the file /wwwroot/yii/demos/blog/protected/data/schema.mysql.sql for that purpose.

Good luck!

Thomas

Quote

The database support in Yii is built on top of PDO, so you can easily switch to different types of DBMS (e.g. MySQL, PostgreSQL) without the need to change the code of your application. In order to use MySQL, you have to configure it in the application configuration (normally in protected/config/main.php) first. Add or modify it, finally it should contain something like that:


array(


    ......


    'components'=>array(


        ......


        'db'=>array(


            'class'=>'CDbConnection',


            'connectionString'=>'mysql:host=localhost;dbname=testdb',


            'username'=>'xyz',


            'password'=>'xxx',


            'charset'=>'UTF8',


        ),


    ),


)

Be sure to set the values for host, dbname, username and password correctly. Yii should now use MySQL for database operations. So you have to fill the database with the tables and data needed for the blog tutorial. You can use the SQL statements in the file /wwwroot/yii/demos/blog/protected/data/schema.mysql.sql for that purpose.

Good luck!

Thomas

Thanks! It's working now. However It is a good idea to post Yii+mysql tutorials in future. I'll request Yii guys to post a CMS and Shopping Cart tutorial using Yii+Mysql.

Thanks again.

I followed this advice but I have a problem. When I try to create the model User using the yiic shell as explained in the docs I get the following error:

This one on the first try:

exception 'CDbException' with message 'CDbConnection failed to open the DB connection: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1' in /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/db/CDbConnection.php:232





Stack trace:


#0 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/db/CDbConnection.php(208): CDbConnection->open()


#1 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/db/CDbConnection.php(187): CDbConnection->setActive(true)


#2 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/base/CModule.php(326): CDbConnection->init()


#3 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/base/CApplication.php(338): CModule->getComponent('db')


#4 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/cli/commands/shell/ModelCommand.php(114): CApplication->getDb()


#5 [internal function]: ModelCommand->generateModel('/var/www/vhosts...', Array)


#6 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/console/CConsoleCommand.php(116): call_user_func(Array, '/var/www/vhosts...', Array)


#7 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/cli/commands/shell/ModelCommand.php(94): ConsoleCommand->copyFiles(Array)


#8 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/cli/commands/ShellCommand.php(136): ModelCommand->run(Array)


#9 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/cli/commands/ShellCommand.php(96): ShellCommand->runShell()


#10 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/console/CConsoleCommandRunner.php(62): ShellCommand->run(Array)


#11 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/console/CConsoleApplication.php(88): CConsoleCommandRunner->run(Array)


#12 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/base/CApplication.php(133): CConsoleApplication->processRequest()


#13 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/yii/1.0.4.920/yiic.php(30): CApplication->run()


#14 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/testdrive/protected/yiic.php(7): require_once('/var/www/vhosts...')


#15 /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/testdrive/protected/yiic(4): require_once('/var/www/vhosts...')


#16 {main}

And this one on the second try:

Warning: you do not have a 'db' database connection as required by Active Record.





  unchanged User.php





The 'User' class has been successfully created in the following file:


    /var/www/vhosts/furryds.net/subdomains/lobosgc/httpdocs/testdrive/protected/models/User.php





If you have a 'db' database connection, you can test it now with:


    $model=User::model()->find();


    print_r($model);


The first try doesn't generate the user.php file, but the second one does. If I continue following the tutorial steps and execute "crud User" I get this error:

Error: Table "User" does not have a primary key.

I've double checked this and the table does have a primary key. I dunno what's wrong, any idea?

Hi

I too get an error while creating model class using command.

I am getting the following errors:

can anyone please help me?

It means your CLI php is not configured correctly because it couldn't find those extension library files.

Thank u. Now its working

i run the command  by specifying the path to php.ini like this:

php -c /etc/php5/apache2/php.ini /var/www/yii/framework/yiic shell /var/www/jobless/index.php

PHP Warning:  Module 'mysql' already loaded in Unknown on line 0

PHP Warning:  Module 'PDO' already loaded in Unknown on line 0

PHP Warning:  Module 'pdo_mysql' already loaded in Unknown on line 0

Yii Interactive Tool v1.0 (based on Yii v1.0.4)

Please type 'help' for help. Type 'exit' to quit.

I am avaluating 6/7 PHP frameworks…but I too am having problems getting the tutorial working with MySQL.  I have the following in my main.php:

'db'=>array(

  'class'=>'CDbConnection',

  'connectionString'=>'mysql:host=localhost;dbname=mytestdb',

  'username'=>'testusername',

  'password'=>'testpw',

),

I receive the following error when in shell mode

php -c /usr/local/php5/lib/php.ini /Users/sholroyd/Sites/van/cliff/protected/yiic.php shell /Users/sholroyd/Sites/van/cliff/index.php

>> model User

Warning: you do not have a 'db' database connection as required by Active Record.

Another strange thing is I cannot print out the db element of the components array.  If I add

<p><?php print_r(Yii::app()->components ) ?>

to my /site/index.php I get nothing about the 'db' element of the array.  Yet other modification (like application name or admin email) display correctly.

Exit 'yiic shell' and re-enter it if you make any change to the app config.

Yii::app()->getComponents() returns the currently loaded components (see the API manual).

Quote

I am avaluating 6/7 PHP frameworks...but I too am having problems getting the tutorial working with MySQL.  I have the following in my main.php:

'db'=>array(

  'class'=>'CDbConnection',

  'connectionString'=>'mysql:host=localhost;dbname=mytestdb',

  'username'=>'testusername',

  'password'=>'testpw',

),

I receive the following error when in shell mode

php -c /usr/local/php5/lib/php.ini /Users/sholroyd/Sites/van/cliff/protected/yiic.php shell /Users/sholroyd/Sites/van/cliff/index.php

>> model User

Warning: you do not have a 'db' database connection as required by Active Record.

Another strange thing is I cannot print out the db element of the components array.  If I add

<p><?php print_r(Yii::app()->components ) ?>

to my /site/index.php I get nothing about the 'db' element of the array.  Yet other modification (like application name or admin email) display correctly.

Same problem here. This is the output of Yii::app()->getComponents():

This is seriously driving me crazy; I'd like to start developing with this framework due to it's capabilities, but I can't get past this point. I've been looking everywhere for this but can't find a solution :confused:

Double posting with a fix that worked for me.

I just appended ‘emulatePrepare’=>true to my conection string (as seen here) and it worked.

I've read the documentation about this property, and I think that an informative note should be added to the Yii guide about this, as there isn't any mention about it on the "Creating First Yii Application" tutorial.

In answer to the original request, I found this mysql tutorial with some googling: Yii Framework + MySQL

[Edit: link fixed, thanks Colt]

The link is broken… :)