A set of widgets for displaying data is provided by Yii. A single record is displayed by DetailView and a table of records is displayed by ListView and Grid View. The List View and Grid View can also be used for filtering, sorting and pagination.
Initially a Database has to be prepared for using the Yii Data Widgets
Step 1 − A new database is created and it can be prepared in two ways:
Step 2 – The database connection is configured in the config/db.php file. The configuration used is:
Step 3 – The command run./yii migrate/create test_table in the root folder creates a database migration for managing the DB. The migration file appears in the migration folder of the project root.
Step 4 – The migration file m160106_163154_test_table.php is modified by the code:
A user table is created with the above migration, with the fields – id, name and email and few demo users are added.
Step 5 − Inside the project root run./yii migrate to apply the migration to the database.
Step 6 – A model is created for our user table. For the sake of simplicity, use the Gii code generation tool. Open up this url: http://localhost:8080/index.php?r=gii. Then, click the “Start” button under the “Model generator” header. Fill in the Table Name (“user”) and the Model Class (“MyUser”), click the “Preview” button and finally, click the “Generate” button.
The MyUser model appear in the models directory.
For displaying the data of a single model, DetailView Widget is used. The $attributes property enables in defining which model attribute to be displayed.
Step 1 − The actionDataWidget method is added to the SiteController.
In the above code, the first MyUser model is identified and passed it to the datawidget view.
Step 2 − A file datawidget.php is created inside the views/site folder.
Step 3 − Visit http://localhost:8080/index.php?r=site/data-widget,and the usage of the DetailView widget appears as:
Yii Related Interview Questions
|PHP Interview Questions||Zend Interview Questions|
|PHP and Jquery Interview Questions||PHP+MySQL Interview Questions|
|Sybase Interview Questions||Dbase Interview Questions|
|Magento Interview Questions||PHP5 Interview Questions|
|WordPress Interview Questions||CakePHP Interview Questions|
|CodeIgniter Interview Questions||Firebase Interview Questions|
|SQLite Interview Questions||PHP7 Interview Questions|
|Object Oriented Programming in PHP Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.