Display only records related to author

Hi,

I’m new to the yii framework. I installed the advanced template and I generated a CRUD using the Gii tool.

My app is able to register new users and log them into the backend. Each user is able to create Events and the events are shown on the GridView provided with the CRUD generated files. I want to make the grid view only show the events that each author created (SELECT * from event, user where event.id_author = user.id and user.id = “some_id”) but I can’t find where or how to modify the initial data that loads with the GridView.

I know it should be very simple but I have not been able to find any information about it.

I supose that it should be here?




public function actionIndex()

    {

        echo Yii::$app->user->identity->id;


        $searchModel = new EventoSearch();

        

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }



Thanks in advanced!

i think you should have created_by field in side EVENT table so when ever any user create event it will save his user_id to event table so you can find easily events of login user.

Well, the field id_author is the same as you suggested… it references the user ID from the table “user”, the thing is I don’t know how to add this condition to the selected results… at this time it just outputs all the data in the table Event

You need to add $q->where([condition]) (See ActiveQuery::where) to your …\models\EventoSearch.php class in the method search.

in your search model of event write below




$user_id=Yii::$app->user->identity->id;

$query = Event::find()->where(['author_id'=>$user_id]);



Thank you very much!!!! that’s what I was looking for. Can’t believe it was that simple.