Yii::app()->user to act as in Yii2 (have access to user object)

Is it possible for Yii::app()->user to act as in Yii2 - have access to user object?

I have a task to remove (if possible) this type of query from the app:

User::model()->findByPk(Yii::app()-->user->id);

I recommend you read
Data Access Objects (DAO)
https://www.yiiframework.com/doc/guide/1.1/es/database.dao

and for queries
https://www.yiiframework.com/doc/api/1.1/CDbCommand#query-detail

how are you doing it works. But you could have code injection. be careful with it. It was something that cost me to learn.

And depending on the query, check the relationships you have in your model. There you can reach the access of objects in any table

it should be pretty straight forward, in yii1 user identity class is located under protected/components/UserIdentity.php you can add a $_user property to assign user info and method that returns a user info

class UserIdentity extends CUserIdentity
{
    private $_user;

    public function authenticate()
    {
         // existing code 
         $this->_user = $user;
    }

    public function getInfo()
    {
         return $this->_user;
    }

}

Yii::app()->user->getInfo();  // should give you the user object

Won’t that work only on login, when the authenticate is called?

ah okay I get what you mean I may have misunderstood your initial post, if you want to preserve the state across requests and not query database you need to persist the user object in session which I would not recommend, alternatively you can override the webuser component

// 1. update config/web.php

'user'=>array(
	// add this line here
	'class' => 'application.components.WebUser',
	'allowAutoLogin'=>true,
),

// 2. create protected/components/WebUser.php file 

<?php

class WebUser extends CWebUser
{
	public $_info = null;

	public function getInfo()
	{

		// Note: query will fail if user is not logged in, perhaps throw in a login check as well

		// only query user table if it loaded already
		if ($this->_info === null) {
			$this->_info = User::model()->findByPk(Yii::app()->user->id);
		}

		return $this->_info;
	}
}

// 3. usage

<?php Yii::app()->user->getInfo(); // would return the user object which is queried