Reputation: 25
In my _form.php
I want to create a dropdown that contains names of relevant users (relevant = users with the role "releaseManager".
This is the code:
echo $form->field($model, 'Assignee')->dropDownList([$assignees = Yii::$app->db->createCommand('SELECT username FROM user, auth_assignment WHERE id=user_id AND item_name="releaseManager"')
->queryAll()]);
The problem is that along with the correct names I also get numbers.
For example: I have 2 relevant names, so I get 2 rows of 0 before the first name and a row with "1" before the second. As if it states the place in the array or I don't know what..
I tried using queryScalar()
but then I get only one name instead of 2.
I will really appreciate your help.
Upvotes: 1
Views: 482
Reputation: 133360
Could be you need ArrayHelper::map()
use yii\helpers\ArrayHelper;
<?php
echo $form->field($model, 'Assignee')->
dropDownList(ArrayHelper::map(Yii::$app->db->
createCommand('SELECT username FROM user, auth_assignment WHERE id=user_id AND item_name="releaseManager"')
->queryAll(), 'username', 'username'));
?>
Upvotes: 1
Reputation: 9358
For re-usability we can also create separate method in model:
public function getUserNames()
{
$usernames = Yii::$app->db->createCommand('SELECT username FROM user, auth_assignment WHERE id=user_id AND item_name="releaseManager"')
->queryAll();
$result = yii\helpers\ArrayHelper::map($usernames, 'user_id', 'username');
return $result;
}
Form
echo $form->field($model, 'Assignee')->dropDownList(PathTOModel/ModelName::getUserNames())
Upvotes: 0