Coding Practices and Examples for Samarth
Identity Management
Use Yii::$app->user->identity->assigned_to
for:
- Reason: "AssignedTo" field in User table consists of EmployeeId in case of Employee type of Account and "OrganigramID" in case of Administrative type of Account.
- Use
SecurityHelper::validateData($id)
for decoding user ids
Identity control for GridView
Use $dataProvider->query->andFilterWhere(['exam_session_id' => $session])
for:
Reason: For filter in Data Grid View use below syntax:
$user_id = SecurityHelper::validateData($id);
$searchModel = new PublicProfileSearch();
$searchModel->ems_profile_id = $user_id;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andFilterWhere(['ems_profile_id' => $user_id]);
Use following code snippet in model for auto filling created at/by and updated at/by fields
As a standard practice place this before the attribute label block
BlameableBehavior automatically fills the specified attributes with the current user ID.
public function behaviors()
{
return [
[
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'created_by',
'updatedByAttribute' => 'updated_by',
],
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}