I've done some reading and as of now I know of 3 ways to achieve this.
1: Using PDO::FETCH_KEY_PAIR
Example:
$Query=Yii::app()->db->createCommand('SELECT col1,col2 FROM '.static::tableName().' ORDER BY '.static::tablePrefix().'_id');
$Query->setFetchMode(PDO::FETCH_KEY_PAIR);
return $Query->queryAll();
Which works splendidly, but... only for 2 columns. (BTW, is there a any other way to pass the fetch type? CDbCommand class and its query functions only accept true/false.
2: Using PDO::FETCH_GROUP|PDO::FETCH_ASSOC
+ array_map('reset', $result)
Example:
$Query=Yii::app()->db->createCommand('SELECT users.user_id,summoners.summoner_name,users.user_login FROM `participants` INNER JOIN `users` ON participant_id=user_id INNER JOIN `summoners` ON user_id=summoner_user_id WHERE participants.tournament_id=1 AND summoners.summoner_region=\'eune\'');
$Query->prepare();
$Query=$Query->getPdoStatement();
$Query->execute();
$Result=$Query->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
return array_map('reset', $Result);
Which is weird, confusing and seems really redundant and ineffective. There is probably some slightly easier way without extracting the statement from Yii's CDbCommand, but still.
3: Using query()
+ foreach
Example:
$Query=Yii::app()->db->createCommand('SELECT users.user_id,summoners.summoner_name,users.user_login FROM `participants` INNER JOIN `users` ON participant_id=user_id INNER JOIN `summoners` ON user_id=summoner_user_id WHERE participants.tournament_id=1 AND summoners.summoner_region=\'eune\'');
$Data=$Query->query();
foreach ($Data as $row)
foreach ($row as $k=>$v)
if($k!='user_id') $Result[$row['user_id']][$k]=$v;
return $Result;
So the 3rd option seems to be the way to go, but don't we have a fetch type for that? I mean... it's 2014, not 1990.