findOne doesn't return ActiveRecord?

I’m switching from yii1 to yii2 and have a small question, I’m currently trying to access a users email with different ways:

#1

$user = new User();

$user->email // this works

#2

$user = User::findOne($id)->email // this also works

#3

$user = User::findOne($id);

$user->email // could someone explain me why I can’t access the email of the model here?

because of identityClass .

My link .

I create a another model for the user table like UserModel using Gii for accessing the data from user table.

I don’t think the identityclass is the problem here, because I have the same issue no matter what model I use.

Hi,

Which version of Yii are you using?

Do you receive any error?

Or is it possible that you have simply forgotten to echo the variable?

I have just tested in app/views/site/about.php




use app\models\User; 


$user = User::findOne(1); 

echo $user->email;




Result:

Regards

It’s very strange. I also think that this should work.

Probably you have to give us more detailed code explaining your problem.

Sorry guys, it seems this was related to phpstorm, somehow it doesn’t show the email property, but when executing the code it’s working fine. Haven’t had something like that before.

It’s because phpStorm has no measure to know what class the variable “$user” is.

Try to tell phpStorm the variable type by a comment.




/* @var $user User */ // this tells phpStorm the variable $user is of User model

$user = User::findOne($id);

echo $user->email;