Get data in yii using findByPk or findByAttributes
Select an model base on pk
you can use this on any model that you had generated
$var = Users::model()->findByPk(Yii::app()->user->id);
We can get the data that we want by simply calling our field name that is on our table for example
echo $var->name;
note that the word 'name' is on our user table that why we can call it.
Select an model base on field attribute
In this case, if you dont know the primary key of what you are going to query but knows part of the field. you can use findByAttributes instead of using by findByPk
$var = Post::model()->findByAttributes(array('first_name'=>'juan'));
//select an model base on attribute with more settings
$var = Users::model()->findByAttributes(
array('first_name'=>'juan','last_name'=>'luna'),
'status=:status',
array(':status'=>1)
);
No comments:
Post a Comment