Professional CodeIgniter

(singke) #1

Chapter 7: Improving the Dashboard


211


The products_colors and products_sizes tables keep track of which selections have been made for any
product. Remember, any given product can have multiple colors and sizes associated with it. Each of
these tables needs to have one field that points back to products and another field that points back to
either colors or sizes.

Here is the products_colors table. Notice that both the product_id and color_id fields are primary keys.

CREATE TABLE ‘products_colors’ (
‘product_id’ INT NOT NULL ,
‘color_id’ INT NOT NULL ,
PRIMARY KEY ( ‘product_id’ , ‘color_id’ )
);

And here is the products_sizes table:

CREATE TABLE ‘products_sizes’ (
‘product_id’ INT NOT NULL ,
‘size_id’ INT NOT NULL ,
PRIMARY KEY ( ‘product_id’ , ‘size_id’ )
);

Deleting References to Legacy Colors and Sizes


Now that you have your new tables, drop the colors and sizes fields from the Products table, and remove
any references to these defunct fields in your MProducts model, particularly the addProduct() and
updateProduct() functions.

ALTER TABLE ‘products’
DROP ‘sizes’,
DROP ‘colors’;

Creating New Models


Right now, you ’ re going to create two basic models without any functions in them just so you ’ ll have
them. Then you ’ re going to add your two new models to the autoload list.

Here ’ s the MColors model (/system/application/models/mcolors.php):

< ?php
class MColors extends Model{

function MColors(){
parent::Model();
}
}//end class
? >
Free download pdf