如何在yii find()中输入条件?

时间:2015-09-21 13:15:01

标签: php mysql yii find conditional-statements

我有两张表AccountUser

Account表包含account_idusernamepassword

User表包含user_idaccount_idfirst_namelast_name

我想要的是yii显示登录的user表的内容,所以我使用了这段代码

$user= Yii::app()->user->id; //to get the account_id of user logged in
$userModel = User::model()->find(array('condition'=>'account_id' == $user));
//to find data in the user table with the account_id similar to the
//account_id of the one logged in
print_r($userModel); //to check if I got the correct data

但由于某些原因,无论谁登录,print_r($userModel)都会返回account_id == 1

的数据

请帮助:/

1 个答案:

答案 0 :(得分:1)

如果您使用的是Yii 1,可以尝试使用findByAttribute

$userModel = User::model()->findByAttributes(array('account_id'=>$user));