How to implement User viewing his created records in yii2

I am using yii2 basic application template

I have Groupdetails CRUD application.

I have auth_item, auth_item_child, auth_assignment and auth_rule. and generated the CRUD’s for the same.

There are two roles admin and fieldofficer.

In auth_item table, I created two roles namely admin and fieldofficer

In auth_assignment table, I assigned the admin role to Employee having id 29

and similarly assigned fieldofficer role to Employee having id 13 and 28.

In Groupdetails controller, I have foll access rules:

return [

        'access' => [

        'class' => \yii\filters\AccessControl::className(),

        'only' => ['create', 'view' , 'update' , 'delete'],

        'rules' => [

      // allow all actions to Admin role users


              'allow' => true,

              'actions' => ['create', 'view' , 'update' , 'delete'],

              'roles' => ['admin'],


      // allow create , view, update actions to FieldOfficer role users


          'allow' => true,

          'actions' => ['create','update','view'],

          'roles' => ['fieldofficer'],


      // everything else is denied



Every thing is working fine.

Now I want that when fieldofficer having id 13 creates two group, then he should be able to view and update only his created two groups.

How to accomplish this?

Do we need to insert rule in auth_rule table or should we need to create Rule class? Explain step by step.

You’re going to have to define your own rules. The link has everything you need to know on how to do it

Have a look at the BlamableBehavior.

Add a ‘created_by’ field in the table. In create action set it to the user->id (blamable). In controller update/delete/etc action add a ->where([‘create_by’ => Yii::$app->user->id]) to $model->find().