Dubby
Dubby

Reputation: 2422

How to pass a variable to a model in Yii2?

I have created a beforeSave() method in one of my models to update field client_id with the client id of the requesting user ($user->client_id). However the model doesn't know who the current user is.

I could use Yii::$app->user->id but I think this is bad practice and think there must be a better way. Of course I could pass the client_id as a hidden field in the form but this would be a security issue.

Any ideas?

Upvotes: 2

Views: 1004

Answers (1)

Double H
Double H

Reputation: 4160

You could use BlameableBehavior of Yii2... client_id ..configure it as createdByAttribute or updatedByAttribute, your choice.

Upvotes: 2

Related Questions