[SOLVED] How to sort a merged array of ActiveRecords

Hi all,

I’ve created a recursive function that returns members from a specific team recursively. The recursion goes into sub teams and returns those members as well.




// --- TeamController ---

public function getMembers($recursively = true)

{

	if ($recursively)

	{

		$members= $this->members;

		foreach ($this->subTeams as $subTeam)

			$members= array_merge($members, $subTeam->getMembers(true));

		return $members;

	}

	else

		return $this->members;

}



Database Tables:

Members: id, userId, teamId

Users: id, name, email

Team: id, name

Member relation


return array(

	'user' => array(self::BELONGS_TO, 'User', 'userId'), ...

The problem is that the array is not sorted. I want to sort the array by Users.Name and give it to CHtml::dropDownList(). I was playing with CArrayDataProvider but couldn’t figure out how to sort on a relational table.

Any ideas would be greatly appreciated!

Update:

This one actually sorts the array, but guess what? The array contains less data! How come?




$dpr = new CArrayDataProvider($members, array(

		'sort' => array(

			'attributes' => array('name' => 'user.name'),

			'defaultOrder' => 'name'

		)

	));

	$members= $dpr->getData();



Okay, I finally figured what was going on.

So relational sort has to specify this way:




'sort' => array(

	'attributes' => array('name' => 'user.name'),

	'defaultOrder' => 'name'

)



The pagination is enabled by default and the pageSize is set to 10. So I had to disable the pagination manually as follows:




'pagination' => false



The full working code:


$dpr = new CArrayDataProvider($members, array(

	'sort' => array(

		'attributes' => array('name' => 'user.name'),

		'defaultOrder' => 'name'

	),

	'pagination' => false

));

$members= $dpr->getData(true);