posted by kevin on July 3, 2009

Now that we have our MySQL connection set up, it's time to use it.

The way the Yii Framework works with data is you create 'Models' which define the relations, rules, and other properties. As of Yii 1.0.6 there is a new feature that will create all of your models with one command and if you have your foreign keys set up it will automatically create your table relations for you.

If you're unfamiliar with foreign keys, basically what they do is say this field should match another table's key.

If you can't use foreign keys, or just don't want to, you can still use the same approach, the only difference is then you actually have to set your relations in your models manually which really isn't difficult, but using foreign keys will ensure better data integrity, and you might as well make it easier on yourself from the start. :)

So first we'll go ahead and set up our database.

We're going to keep this really simple just for demonstration purposes.

We'll create 4 tables. They will be: Brand
Product
Category
ProductCategory

We will set three foreign key relationships.

 
Product.brandId => Brand.id
ProductCategory.productId => Product.id
ProductCategory.categoryId => Category.id
 

One important thing to not is that not all storage engines support foreign keys, so you need to use one that does. InnoDB does support foreign keys, so that is the engine I will be using.

Click Here To Download My Sample DB Dump.

If you want to follow along, go ahead and download and load up your database using the dump provided above.

Now we're going to create our models.

The Yii Framework comes with a shell command that is really great, and will generate your models for you.

Now let's open up our terminal and let yii's shell script generate our models for us.

 
cd /data/web/vhosts/mysqltest.com/protected
./yiic shell ../index.php [This is running the yii shell using the configuration from your main index file]
model * [This will tell you what models it will set up for you. Go ahead and confirm it.]
 

If everything works properly you should now have some new classes in your protected/models folder which we will be using in the next micro-tutorial.

8 comments to "Yii Framework + MySQL Part 2 (Creating Models)"

#29
Deyon says:
July 8, 2009 at 06:20 pm
dude, your tutorials are better then the yii docs be cause they give you messed up path names and leave you to figure it out. So far i'm following your tut and its going smooth. I would like to request more Yii tuts please.
#31
July 8, 2009 at 06:43 pm
@Deyon I'm glad everything is working for you. The documentation on the Yii site is ok, but I thought I can add some useful insight since I'm not on the Yii development team, and I've spent some time banging my head on the table at times wondering why exactly things aren't working correctly. I'll continue to post on Yii related things, but if you have any specific things that you'd like to know about, or need a little help, please let me know and I'll probably add them to my list of micro-tutorial ideas.
#75
Joe says:
November 3, 2009 at 08:18 pm
I'm trying to load the your sample db dump and it works up until the ALTER statements. I get this: Error SQL query: -- -- Constraints for dumped tables -- -- -- Constraints for table `Product` -- ALTER TABLE `Product` ADD CONSTRAINT `product_ibfk_1` FOREIGN KEY ( `brandId` ) REFERENCES `brand` ( `id` ) ; MySQL said: Documentation #1005 - Can't create table 'mysqltest.#sql-125_67' (errno: 150) (Details...)
#79
Dave says:
November 16, 2009 at 03:45 pm
I'm getting the same error as Joe... I'm using MySQL 5.1.37. Any help would be great... Thanks
#80
November 16, 2009 at 03:52 pm
I apologize for this. You'll notice at the bottom of the sql import script is where it's dealing with the foreign keys.
 
--
-- Constraints for table `Product`
--
ALTER TABLE `Product`
  ADD CONSTRAINT `product_ibfk_1` FOREIGN KEY (`brandId`) REFERENCES `brand` (`id`);
 
--
-- Constraints for table `ProductCategory`
--
ALTER TABLE `ProductCategory`
  ADD CONSTRAINT `productcategory_ibfk_2` FOREIGN KEY (`categoryId`) REFERENCES `category` (`id`),
  ADD CONSTRAINT `productcategory_ibfk_1` FOREIGN KEY (`productId`) REFERENCES `product` (`id`);
 
 
So until I can load the lastest version of MySQL and get to re-creating the error, see if you can go about creating your foreign keys after importing the main part of the database. Maybe the syntax changed or something like that. I'm not totally sure at this point.
#81
Dave says:
November 16, 2009 at 04:02 pm
Sorted it... Case sensitivity problem... Changed 'brand', 'category', 'product' to 'Brand', 'Category', 'Product' respectively and the problem has gone away. Thanks for a good tutorial.
#82
November 16, 2009 at 04:06 pm
Thanks Dave,

I've updated the sample dump code. :) Shame on you phpMyAdmin SQL Dump!
#102
kareem says:
January 18, 2010 at 06:37 am
i have a prob that when i want to create model throught shell command i got this error : Warning: YiiBase::include(PDO.php): failed to open stream: No such file or direc tory in C:\wamp\framework\YiiBase.php on line 338 Warning: YiiBase::include(): Failed opening 'PDO.php' for inclusion (include_pat h='.;C:\wamp\www\yiitst\protected\components;C:\wamp\www\yiitst\protected\models ;C:\php5\pear') in C:\wamp\framework\YiiBase.php on line 338 Fatal error: Class 'PDO' not found in C:\wamp\framework\db\CDbConnection.php on line 296 any help ??? im using wamp1.6 server
Bookmark and Share

Leave a Comment

Your email address will not be published.

(You can enclose code in <php></php> blocks.)

You may use Markdown syntax.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.