farhad2161
farhad2161

Reputation: 31

how prevent yii2 to execute select query when ever call Yii::$app->user->id

When ever I use

Yii::$app->user->id

or

Yii::$app->user->isGuest 

Yii2 execute SELECT * FROMuserWHEREid='4' .

How can I prevent this repetitive query in order to optimize my web app?
Caching or defining global session variable and something like that.

Upvotes: 0

Views: 663

Answers (1)

took
took

Reputation: 174

Enable one of the supported cache storages and replace the code return static::findOne(['id' => $id]); you mentioned in your comment with

$db = self::getDb();
return static::getDb()->cache(function ($db) use ($id) {
    return static::find()->where(['id' => $id])->one();
});

For more details, see http://www.yiiframework.com/doc-2.0/guide-caching-data.html

Upvotes: 0

Related Questions