Friday, December 21, 2012

How to override default database value in form field?


Creation of form in Yii is very easy. You can create it very easily. But sometime it will be headache for you if you are unknown with some features.

Ex : Suppose you have some default value for some column in a table. And you want to create a Create form. Some time it is good to display default values but not always. So, this default value can irritate you.

How to remove default value???

Simple, go to the model class corresponding to your form and add

public $columnNameOne = null;
public $columnNameTwo = null;

Note : You have to include these lines on class level not method level.

Now the above columns will not show default values in form.

How to generate crud, models or forms in Modules


How to generate crud, models or forms in Modules ?
This is simple but tricky for novice. If you have good understanding of path in YII (or in any computer language) then you can easily understand it. 
Okey , let's start :
These are keywords which are use in Yii framework :  
  • system : Refers to the Yii framework directory.
  • zii: Refers to the Zii library directory.
  • application: Refers to the application's base directory. Using this you can point your base directory. You can say this will be root directory of all directories of your project.
  • webroot: Refers to the directory containing the entry script file.
  • ext: Refers to the directory containing all third-party extensions.
Example: Suppose you have to access models of modules 'Group'. Let say directory structure is :

/opt/lampp/htdocs/TestProject/modules/model/testmodle.php

and you want to get reference of "testmodel.php" file in your program. One solution is you just give the full path of "testmodel.php" . But suppose after sometime you want to move your project to another machine then you there you will get error. Because system will never find the above path.

So, what is the solution???

Think...
Think...
Again Think...

Ya, you got it. You need to provide relative path. Yii framework provides very good solution for this. Now you can refer the above describe alias path. 

Solution : application.module.model.testmodel.php

 I think it is clear for you. Now come to the core question. How we can generate models/crud/form under a modules.   

  1. Create a module using Gii tool.
  2. Register your module name in the config main.php 
  3. Create a model via Model Generator. Fill in the fields and put this in Model Path : application.modules.[module-name].models
  4. Create crud via Crud Generator. Fill in the fields and put this in Model Class: application.modules.[module-name].models.[model-name]
The above example was for models. Similarly you can use for crud or form.