Skip to main content

Building with YII2

Create Prototypes and Wireframes

Create wireframes of the interfaces you want to build. We recommend using miro for wireframing. You can first start with low fidelity prototypes and then convert them to web app flowcharts and wireframes. References in order :

Configure Samarth! YII2

Visit https://boilerplate.samarth.ac.in and clone the customised YII2 setup. Configure just like standard YII2 deployment as explained in guide for Installing and Running YII2

Create Data Skeleton for your app

Follow these guidelines to create the database for the interfaces you need to build:

  1. The database, table and column character set should be utf8mb4 and collation should be utf8mb4_unicode_ci
  2. Table name structure <modulename><table_name>
    • prg_course_list : prg for programme module and course_list for course list
    • ems_employee_details : ems for employee module and employee_details for employee details
    • student_record : student for student module and record as table name
  3. Multiple words in table name and column name must be separated by underscore [_]
  4. All the letters in table and column name must be lower case
  5. Every table should contain
    • created_at type integer value will be timestamp,
    • updated_at type integer value will be timestamp,
    • created_by type integer value will be user id,
    • updated_by type integer value will be user id,
    • updated type timestamp default value will be CURRENT_TIMESTAMP
  6. For varchar columns if the value is large the length can be configured as 192, 255, 500, 1000 based on requirements.
  7. Every table should have an id column as primary key auto increment
  8. Foreign keys should have id appended in the end in foreign table eg student_id, programme_id, ou_id
  9. The data type of foreign key should be same as parent table
  10. For storing financial data use decimal(10,2) data type
  11. For the status tracking status column can be added the default value should be DRAFT and values can change states from DRAFT, PENDING, REJECTED, DELETE, DISABLED, PUBLISHED

Create Models using Gii

To start Gii, go to <path_to_entry_script>/gii if pretty URLs are enabled or <path_to_entry_script>?r=gii, otherwise.

The Model Generator creates an Active Record class of the specified table.

Active Record provides an object-oriented interface for accessing and manipulating data stored in databases. An Active Record class is associated with a database table, an Active Record instance corresponds to a row of that table, and an attribute of an Active Record instance represents the value of a particular column in that row. Instead of writing raw SQL statements, you would access Active Record attributes and call Active Record methods to access and manipulate the data stored in database tables.

Prerequisites:

The table for which the ActiveRecord class has to be generated must be present in the database.

Steps:

  1. Click on the 'Start' button under Model Generator
  2. Select a table name from the suggested list or type the table name in the Table Name field
  3. The Model Class Name field will be automatically populated but can be edited
  4. The Standardize Capitals checkbox field indicates whether the generated class names should have standardized capitals. For example, table names like SOME_TABLE or Other_Table will have class names SomeTable and OtherTable, respectively. If not checked, the same tables will have class names SOMETABLE and OtherTable instead
  5. The Singularize checkbox field indicates whether the generated class names should be singularized. For example, table names like some_tables will have class names SomeTable
  6. The Namespace field indicates the path to where the ActiveRecord class should be generated, for example, app\models
  7. The Base Class field should have the fully qualified namespaced class name from which the new Active Record class will be extended/inherited. By default, the value is set to yii\db\ActiveRecord
  8. The Database Connection ID field should have the ID of the DB Application component, usually, mentioned in app\config\web.php
  9. The Use Table Prefix checkbox field indicates whether the table name returned by the generated ActiveRecord class should consider the tablePrefix setting of the DB connection. For example, if the table name is tbl_post and tablePrefix=tbl_, the ActiveRecord class will return the table name as {{%post}}
  10. Click on the 'Preview' button to get a list of all the new file(s) to be generated along with actions like create or overwrite (if a file with the same name at the same path already exists)
  11. Finally, click on the 'Generate' button to generate the file(s) shown in the preview
  12. The files should now appear in your application

Create CRUD using GII

  1. On the same dashboard of GII, select CRUD Generator option

  2. In Model Class field, write the relative path (with backslashes) of your model class (generated via Model Generator)

  3. Now in your application structure, make search sub-folder in the models folder : models -> search

  4. In the Search Model Class field, write the relative path (with backslashes) of the search folder created in the previous step + the name of your search model class. Example : if your model class name is Rule then the search model class field would look like relative_path_to_search_folder/RuleSearch

    Note - Make sure you add “Search” to the end of your name as shown above in the example

  5. In the Controller Class field, write the relative path (with backslashes) of the controllers folder + the name of your search model class. Example: if your model class name is Rule then the controller class field would look like relative_path_to_controller_folder/RuleController Note - Make sure you add “Controller” to the end of your name as shown above in the example

  6. Make a sub-folder with the name of your model class (in lowercase) in views folder

  7. In the View Path field, write the relative path of your views folder created in previous step in the following format @app/views/name_of_your_folder

  8. Choose the widget (GridView/ListView) in Widget Used in Index Page field

  9. Select I18N if you want to enable internationalisation through which application can be adapted to various languages and regions without engineering changes

  10. Select Pjax to enable AJAX to your GridView/ListView

  11. Next, click on the Preview button. You will see a list of files to be generated

  12. If you previously created the controllers/your_controller_class_name.php and views/your_view_folder_name/index.php files, check the "overwrite" box to replace them

  13. Click on Generate button. You will see all the newly created files in your application structure

Modify Views as per the wireframes/designs

References

Database Table and Column Nomenclature by @anil.singh