Yii 1.1: manymanyajaxcrudadmin

Giix templates for ajax CRUD code generation of a MANY_MANY Model relationship.


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.


  • Hide index.php from your requests,if you have'nt done so yet.You can find detailed instructions on how to do this here.(Paragraph 6) Also,in urlManager configuration in config/main.php file set
  • Unzip the downloaded file.
  • Copy the js_plugins folder to the root folder of your application,(same level as protected).
  • Copy the img folder folder to the root folder of your application,(same level as protected).
  • Copy the many_many.css file to your application's css folder,(or create the css folder if it does'nt exist).
  • 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).

    • In your config/main.php file include in the import section:
                    //   ...  **other imports**...
                    'ext.giix-components.*', // giix components
                    //   ...  **other imports**..
  • In config/main.php file,in gii configuration, add the path of your giix-core folder like so:

            'password'=>'[YOUR PASSWORD]',      
            'ipFilters'=>array('','::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.

  • Click GiixModel Generator.Type the product table name in the Table Name field.
  • Important!Accept the model class name suggested by gii, don't try any other fancy names like MyProduct,My_Product etc.Just keep Product. This applies to the other two tables as well.
  • Click Code Template and choose the many_many_product template.
  • Preview if you want,and then Generate.
  • Your Product model class has been generated.
  • Follow the previous steps to genarate the Category model class in the same way: Type category in the table field and choose the many_many_category template.
  • 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:

  • Click GiixCrud Generator.
  • Type Product in the Model class field and keep the suggested controller ID,usually the same as the Model class name with first letter lowercase.
  • Choose the many_many_ product Code Template.
  • Preview if you want,and then Generate.
  • 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.



  • 1.1 December 15th 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....

Total 3 comments

#10698 report it
ChessSpider at 2012/11/16 07:34am

Doesn't work.. have to disable error_reporting to get a partial working extension, only to have it cause a php-fail 100 lines further in the view due to a missing class-constant. There is no default actionIndex, no PHPdoc-style documentation.

#6760 report it
drumaddict at 2012/02/03 08:11am

Change your php.ini settings of error reporting to something less than STRICT.I know it's not best practice but I did not develop this extension with strict error reporting settings.

#6753 report it
Yureshwar Ravuri at 2012/02/03 01:34am
Unable to create the CRUD for category getting the following error
Undefined variable: model in 
online 32
<h3 id="update_header">Update <?php echo $this->modelClass;?> <span  style="color:#4079C8"><?php echo $model->name?></span></h3>

even if changed to this

<h3 id="update_header">Update <?php echo'<?php echo $this->modelClass;?> <span  style="color:#4079C8"><?php echo $model->name?>';?></span></h3>

I am getting this error

Undefined variable: category_id 
online 67
 <?php    if ($category_id == 'all'): ?>

Could you please tell me on how to solve this.

Leave a comment

Please to leave your comment.

Create extension