Starter
Starter

Reputation: 11

How to view own entities in yii2?

I have User table which has a field called 'society_id' Which defines which society the user belongs to. Similarly, I have 'society_id' field in another table called 'expense_details' which identifies the society_id of the user who has entered the data in 'expense_details'.

this is my user table

https://i.sstatic.net/1hBky.png

this is my expense-details table

https://i.sstatic.net/Z3cQU.png

I know we can access the society_id of logged in user like this :

I want Logged in users to access their view but I want the user not to access data from table 'expense_details' related to other users with change url.

I know we can get society_id of logged in user like this Yii::$app->user->identity->society_id But I am wondering how can i use it here and what changes i am supposed to make in my actionView and/or Model.

Here is my Expensedetails view controller.

 public function actionView($id) {

    $details = \app\models\ExpenseDetails::find()->where(['expense_id' => $id])->all();
    $searchModel = new \app\models\ExpenseDetailsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $dataProvider->query->where("expense_id=$id");

    return $this->render('view', [
                'model' => $this->findModel($id),
                'details' => $details,
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
    ]);
}

PS : English is not my native language. I am newbie to yii2 and stackoverflow, please excuse me for the mistakes. Thanks.

Upvotes: 0

Views: 112

Answers (2)

Ramisha Mukhtar
Ramisha Mukhtar

Reputation: 361

As each user has society_id which can be same for any 2 user's records, so when you will fetch the table like this:

ExpenseDetails::find()->where(['expense_id' => $id, 'society_id' => Yii::$app->user->identity->society_id])->all();

it will return all the records on specific expense_id and specific society_id but those records would be of multiple users, if you want another condition that one user can not access anothers user's record, you can add "user_id" attribute in "expense_details" table and set the "users" table "id" attribute to it, as per need so you can fetch the records with specific expense_id, society_id and specific "user_id":

ExpenseDetails::find()->where(['expense_id' => $id, 'society_id' => Yii::$app->user->identity->society_id, "user_id" => 1])->all();

or for current logged in user :

ExpenseDetails::find()->where(['expense_id' => $id, 'society_id' => Yii::$app->user->identity->society_id, "user_id" => Yii::$app->user->id])->all();

Upvotes: 0

Starter
Starter

Reputation: 11

I solved it.

In My ExpenseDetails Model

protected function findModel($id)
{
    if (($model =ExpenseDetails::findOne($id)) !== null) {
        return $model;
    }

    throw new NotFoundHttpException('The requested page does not exist.');
}

In My Expensedetails view controller

protected function findModel($id)
{
    if (($model = ExpenseDetails::findOne($id)) !== null) {
        return $model;
    }

    throw new NotFoundHttpException('The requested page does not exist.');
}

Upvotes: 0

Related Questions