Show Data From 2 Level Dependency Of Field With Relation With Searching in yii

Sometimes while getting value from foreign key relation, dependency is more than one level. This post covers how to get value from base table and enable search in gridview.

Consider following scenario:

  1. User table:     Userid, transaction_id,user_type
  2. Transaction : Userid, info_id
  3. Info : info_id, name

Suppose requirement is to show  name of user in user grid. Here , to relate name and userid , there is a 2 level dependency through transaction table.

Here we are not taking name field directly in user table, because, there are different type of user and each type has different info table which contains name.Transaction table  is a master table for each type which has ids like user_id,info_id, address_id etc.

To get name value in user table with search

1)Make a relation with  transaction table in User model.

‘rel_tran’ => array(self::BELONGS_TO, ‘Transaction’,’user_id’),

2) in Transaction model, make a relation with info table.

‘rel_info’ => array(self::BELONGS_TO, ‘Transaction’,’info_id’),

3)Now in search method,

For get Info field’s name in user

$criteria->with = array(‘rel_tran’,’rel_tran.rel_info’);


here,name is a field of info which,that needed to show with relation.

To use other model’s relation we use ‘.’ with relation to that table.

For example,To access name, rel_transaction.rel_info and in compare directly write rel_info.

Declare first_name with public in user model & add in search in rules.

In admin ,gridview,

‘value’ => ‘$data->rel_tran->rel_info->first_name’,

While showing value we use ‘->’ and write both model’s relation name to retrieve the value.For example, here $data->rel_tran->rel_info->first_name.

This way,you are able to work with 2 level relations with searching.

Other way,

It is alternate work around to achieve same functionality:

1) Add a new field of transaction in info.(info_transaction_id)
2) Make relation with transaction

'rel_tran' => array(self::BELONGS_TO, 'Transaction','', 'on' => 't.created_by=user_id'),

here in created by,I have user table id.
user_id is a field in transaction table which has value of user id.
3)Make relation with type1_info


here transaction_id is auto-increment id of transaction table.
4)Declare name as public variable & in search,
In search method

 $criteria->with = array('rel_tran','rel_info');

5)In gridview get value with $data->rel_info->name.
This way,I got name with searching in gridview with 2 level dependency.


Customize default yii application structure

To create yii application, command is ./yiic/webapp app-name.
Application is generated under framework folder and it is as per default yii defined structure.
This guide is to customize the structure of default Yii application generation.

The default webapplication created is copy of  yii/framework/cli/views/webapp.
To customize it, one can change it directly, but it is not recommended way.
We should make a copy of it and save as different name like mywebapp.
New copy should be in same folder  yii/framework/cli/views.
Make all changes as per your file structure and change in code, add components,theme for your application in it.

Open webappcommand.php file from   yii/frameworkcli/commands.
We need to change SourceDir name to our new name.Change following line in code

Now our new yii web application structure is ready.
Run ./yiic webapp app-name,& It will make a application as per your changes.
You can generate application yii default or customize by changing source directory in webappcommand.php file.
Start customizing it!!!
All the best!!!
Have a good day!!!

Yii:Customize default gii base crud

Yii is a MVC Framework of PHP.

Yii framework offers default crud generation feature. If you write your application name/gii, it will show default crud generation operations. Many times we use different template in project than default crud and need to made changes after watch time crud is generated. This is post to customize default crud to get rid of redundant task.
Let’s begin.

When you are writing in your application-name/gii ,you are seeing all generators on index.
This generator code is located at  yii/framework/gii/views/index.php
Here ,each generator link related code located at yii/framework/gii/generators,
We will extend basic crud and do change in it, so we are able to generate default crud in other applications.

To add your crud generator follow below simple steps:
1. Make folder with your custom name,suppose mycrud under yii/framework/gii/generators.
Make Code file,like myCrudCode in mycrud,which extends CrudCode.

2.Make generator in giiextended folder,like MyCrudGenerator, which extends CrudGenerator.
You need to change in $codeModel ,which give path to code file,
here myCode.
Import : Yii::import(‘gii.generators.crud.CrudGenerator’);
Also make it in mycrud,where $codemodel=ext.giiextended.myCrud.myCrudCode
please notice that,if folder name is mycrud,generate will be name of MycrudGenerator,first letter will be capital.

3.Make Folder template in mycrud folder,in which all views and controller  in default folder.Here what you will make changes accordingly future views are generated from your crud.Following views need to be generated as per Yii crud, you can skip 1 or 2 views and change it as per your need, but you need to reflect changes in controller accordingly.

  •   _form.php
  •   _search.php
  •   _view.php
  •  admin.php
  •  controller.php
  •  create.php
  •  update.php
  •  index.php
  •  view.php

4.Make a folder view,put a index.php in it,
copy it form crud(framework/gii/generators/crud/view/index.php) ,change name in h1 tag.

5.In config/main.php,in gii in generatorpath array,add ext.giiextended.
as example:
This will responsible for showing uour customized crud name when you type application-name/gii in url. So try it and you will come out of hustle of each time crud change as new table is generated.
Similaraly,you are able to extend model and form generator also of gii.
Start doing it.All the Best!!!
Have a good day!!!