aslan
(Aslan Shek)
November 7, 2012, 7:48pm
1
hi
here is what i want to do:
i have an ActiveRecord user, and the relations in it:
public function relations() {
return array(
'countAsk' => array(self::STAT, 'Ask', 'learner_id'),
'countAnswer' => array(self::STAT, 'answer', 'learner_id'),
);
}
when i fetch model by using:
$users = User::model()->findAll();
how can i order user in $users by ‘countAsk’, or by ‘countAnswer’, please?
Keith
(Kburton)
November 7, 2012, 8:11pm
2
Does this work?
$criteria = new CDbCriteria;
$criteria->with = array('countAsk');
$criteria->order = 'countAsk ASC';
$users = User::model()->findAll($criteria);
seenivasan
(Chellamnivas)
November 7, 2012, 8:47pm
3
Dear Aslan
Kindly pay attention on the following posts.
1.Sorting on Statistical Relational Attributes. Fetching statistical relational attributes and sorting based on that
2.Filtering on STAT relation CGridView
regards.
aslan
(Aslan Shek)
November 7, 2012, 9:15pm
4
hi @Keith , it doesnt work
hi @seenivasan , i am now reading what you gave me.
Thank you both.
lucianocn
(Luciano Cn)
November 7, 2012, 9:52pm
5
$criteria = new CDbCriteria;
$criteria->with = array('countAsk');
$criteria->order = 'countAsk.id ASC'; //order by some field, like ID.
$users = User::model()->findAll($criteria);
aslan
(Aslan Shek)
November 7, 2012, 10:59pm
6
hi all,
i found a solution. maybe its ugly, but it works
$rank_by = 'answers';
if (isset($_POST['rank_by']))
$rank_by = $_POST['rank_by'];
$us = User::model()->with('countAsk', 'countAnswer')->findAllBySql('select * from tpl_user limit 10');
$userArr = array();
if ($rank_by == 'questions')
foreach ($us as $user)
$userArr[$user->countAsk] = $user;
else
foreach ($us as $user)
$userArr[$user->countAnswer] = $user;
krsort($userArr);
$users = array();
foreach ($userArr as $user)
array_push($users, $user);