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.
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:
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.
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:
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.
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?
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.
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
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.