The application im working on needs to get posts in an order according to the user's preferences ,if the user is not logged in it directly sorts them in descending order.if the user is logged in it should check the User model and get the preferences from the database.and should get posts in a similar way as the facebook news feed for a particular user.How do i do it?
if(Yii::app()->user->isGuest)
{
$criteria=new CDbCriteria(array(
'order'=>'likes DESC',
));}
else{
$criteria=new CDbCriteria(array(
'order'=>'likes DESC',
));
}
$total = Post::model()->count();
$pages = new CPagination($total);
$pages->pageSize = 10;
$pages->applyLimit($criteria);
$posts = Post::model()->findAll($criteria);
$this->render('index', array(
'posts' => $posts,
'pages' => $pages,
));
Something like this? This assumes that you have a model called User, and store prefs in a column called pref, and in this column is the name of the column on which you want to order.
if (Yii::app()->user->isGuest) {
$criteria = new CDbCriteria(array(
'order' => 'likes DESC',
));
} else {
$userId = Yii::app()->user;
$pref = User::model()->findByPk($userId)->pref;
$criteria = new CDbCriteria(array(
'order' => ':pref DESC',
'params' => array(
':pref' => $pref
)
));
}