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:
- The database, table and column character set should be utf8mb4 and collation should be utf8mb4_unicode_ci
- 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
- Multiple words in table name and column name must be separated by underscore [_]
- All the letters in table and column name must be lower case
- 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
- For varchar columns if the value is large the length can be configured as 192, 255, 500, 1000 based on requirements.
- Every table should have an id column as primary key auto increment
- Foreign keys should have id appended in the end in foreign table eg student_id, programme_id, ou_id
- The data type of foreign key should be same as parent table
- For storing financial data use decimal(10,2) data type
- 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:
- Click on the 'Start' button under Model Generator
- Select a table name from the suggested list or type the table name in the Table Name field
- The Model Class Name field will be automatically populated but can be edited
- The Standardize Capitals checkbox field indicates whether the generated class names should have standardized capitals. For example, table names like
SOME_TABLE
orOther_Table
will have class namesSomeTable
andOtherTable
, respectively. If not checked, the same tables will have class namesSOMETABLE
andOtherTable
instead - The Singularize checkbox field indicates whether the generated class names should be singularized. For example, table names like
some_tables
will have class namesSomeTable
- The Namespace field indicates the path to where the ActiveRecord class should be generated, for example,
app\models
- 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
- The Database Connection ID field should have the ID of the DB Application component, usually, mentioned in
app\config\web.php
- 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}}
- 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)
- Finally, click on the 'Generate' button to generate the file(s) shown in the preview
- The files should now appear in your application
Create CRUD using GII
On the same dashboard of GII, select CRUD Generator option
In Model Class field, write the relative path (with backslashes) of your model class (generated via Model Generator)
Now in your application structure, make search sub-folder in the models folder :
models -> search
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
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
Make a sub-folder with the name of your model class (in lowercase) in views folder
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
Choose the widget (GridView/ListView) in Widget Used in Index Page field
Select I18N if you want to enable internationalisation through which application can be adapted to various languages and regions without engineering changes
Select Pjax to enable AJAX to your GridView/ListView
Next, click on the Preview button. You will see a list of files to be generated
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
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