Thursday, 18 December 2014

Create MVC without Database Table in YII

(for beginners) It should have at least One DB MVC... 

Create a model for the table fro this use the link..
http://localhost/app_dir/index.php?r=gii

replace the app_dir with your application directory in the above link..

give the field as gii for the 'r' argument. The password for gii is in the APP_DIR/Protected/Config/main.php 
in the gii module the password for the application is set

When the password provided the page gives you options to create model,crud,modules etc;

for the table create the model first then after select the crud.. (to create model enter the table name)

For this supply the model name.. Then click the generate button this created the crud ..

Now copy the DBmodel and paste in the same folder .. rename the copied version to your user defined model say 'reports'(Reports.php) .

Open the reports model and find the words 'extends CActiveRecord' remove the two words so that the reports model is now a user defined class not mapped to DB. CActiveRecord maps the model to the database table.

Similarly copy the Generated controller and paste it same folder. Rename the copied controller to userdefined controller in our example ReportsController.php

Open the ReportsController.php find the statement 
'class DBTableController extends Controller '

dont remove the extends controller, it should inherit the controller class.. what we need to modify is the name of the class i.e., DBTableController modify it as user defined controller (see the example below)  
'class ReportsController extends Controller'

define the accessrules and create your own actions remember for each action defined there should be a view page.

So now creating our views...

create a folder in views. Give permissions (or copy the DBGenerated view folder and paste in same folder). Name the folder as 'reports' all are small-case. 

In that create the php files for each action defined in ReportsController.... 

Accessing the User Defined MVC..

Once this is all done one can render the action as 

$this->menu=array(
      array('label'=>'Reports','url'=>array('reports/index')),   // renders Index action in reports 
); 

this is the menu item we can use in main menu also in app_dir/protected/views/layout/main.php

No comments:

Post a Comment