This extension provides giix templates for ajax CRUD code generation of a MANY_MANY Model relationship,between Category-like and Product-like Models. The Category-like model is given a nestedset Behavior and administered with jstree plugin. The Product-like model is administered with either a CGridView or a CListView. Clicking on the category nodes of the tree will filter the product records in a CGridView(or CListView). Frontend views are also provided,with only read (view) operation. The forms are rendered in a pop up window (fancybox) and the submission is an ajax process.However because the jquery form plugin is used (see resources) ,it is possible to include a file field and submit the form without a page refresh.This is not ajax though,as the XHR object cannot do this.A technique involving an iframe is used.For more info see jquery form plugin documentation.
Developed and tested with Yii 1.8,may work with other versions too.
Copy the fileimagebehavior,giix-components,giix-core,image,nestedBehavior folders to the extensions folder of your application. Note:If you already have giix installed you only need to copy the templates found in extensions\giix-core\giixCrud\templates and extensions\giix-core\giixModel\templates.(many_many_category,many_many_many_product folders).
'import'=>array( // ... **other imports**... 'ext.giix-components.*', // giix components 'ext.fileimagebehavior.*' // ... **other imports**.. )
In config/main.php file,in gii configuration, add the path of your giix-core folder like so:
'modules'=>array( ...... 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'[YOUR PASSWORD]', 'ipFilters'=>array('127.0.0.1','::1'),// If removed, Gii defaults to localhost only. 'generatorPaths' => array( 'ext.giix-core', // giix generators ), ), ....... )
Design the database schema of the MANY_MANY relationship and create the tables in your database. A sample schema is provided in many_many.sql file. You will need three tables,two for the related models and one pivot table. You can use any name for the tables as long as they are simple:one word,all letters lowercase. For convenience's sake let's call one table product and the other category. In this case the pivot table is usually named product_category. Please read the comments in many_many.sql file to see which table columns are required for this extension to work. The category table will generate a model with nested behavior and will be administered with a tree (jstree plugin) in admin view files. The product table will generate a model administered with either a CGridView or CListView. Clicking on the category nodes of the tree will filter the product records in CGridView(or CListView).
With the tables created ,you can proceed to code generation with gii. Navigate to gii [application root]/gii.
Again the same process for the product_category pivot table,type product_category in the table field and choose the default template this time.
Open the newly generated Category class file and remove ALL rules associated with root,rgt,left and level properties.
With the models generated,proceed to the crud code generation:
Follow the previous steps to genarate the Category crud files in the same way: Type Category in the Model class field and keep the suggested controller ID,choose the many_many_ category Code Template, preview if you want and then generate.
A minor modification for the newly generated _form.php file in the views/category folder is needed,open the file and follow the instructions in the comments right after the form->beginWidget.
This is it.Navigate to [application root]/category/admin_grid.Start by creating a root category node,then create more nodes. Create Products,and associate them with categories or vice versa.
The image fields associated with the models do not depend on the database schema,their functionality originates in the fileimagebehavior. If you want to remove this feature,do the following.Open the model class file and delete the *_image property declaration in the beginning of the class file,the associated rule in the rules function,and the *ImgBehavior configuration.Open the _form.php files and remove the file fields.Last,delete the img folder.
Spiros "drumaddict" Kabasakalis,December 11th 2011.
Fixed some wrong urls for the grid buttons in admin_grid.php and index_grid.php files.Not a big deal,you have probably fixed this already....