Ar, Order By 'relations'

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?

Does this work?




$criteria = new CDbCriteria;

$criteria->with = array('countAsk');

$criteria->order = 'countAsk ASC';

$users = User::model()->findAll($criteria);



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.

hi @Keith, it doesnt work :(

hi @seenivasan, i am now reading what you gave me.

Thank you both.


$criteria = new CDbCriteria;

$criteria->with = array('countAsk');

$criteria->order = 'countAsk.id ASC'; //order by some field, like ID.

$users = User::model()->findAll($criteria);

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);