Getting current userlist in adminhtml select

26 views Asked by At

I'm trying to get the current (complete) userlist in a adminhtml select form. I've got the select form and a custom source model. Everything works correctly if I set up a manual Array.

However, I've got a hard time trying to figure out how to get it automatically. All I know is that I'm doing this completely wrong but I can't think of the way it has to be. I've found that I can get the current userlist by this:

$roles_users = Mage::getResourceModel('admin/roles_user_collection');
  foreach($roles_users as $roleuser):
   $user = Mage::getModel('admin/user')->load($roleuser->getUserId());
   echo $user->getUsername();
  endforeach;

And that I can get info in the select like this:

public function toOptionArray()
{
    $themes = array(
        array('value' => 'user 1', 'label' => 'user 1'),
        array('value' => 'user 2', 'label' => 'user 2'),
        array('value' => 'user 3', 'label' => 'user 3'),
    );

    return $themes;
}

Even though I seem to completely fail to get magento picks in array form:

public function toOptionArray()
{
    $rolesUsers = Mage::getResourceModel('admin/roles_user_collection');

    foreach ($rolesUsers as $roleuser) :
        $user = Mage::getModel('admin/user')->load($roleuser->getUserId());
        $users = array(
            array('value' => $user->getUsername(), 'label' => $user->getUsername()),
        );

        return $users;
    endforeach;
}

The idea is that I get it like this, but through magento and not manually.

enter image description here

1

There are 1 answers

0
Chris van der Geld On

Fixed it through the Magento way:

/**
 * @return array
 */
public function toOptionArray()
{
    /** @var \Mage_Admin_Model_Resource_User_Collection $userCollection */
    $userCollection = Mage::getResourceModel('admin/user_collection');

    return $userCollection
        ->addFieldToSelect('user_id', 'id')
        ->addFieldToSelect('username', 'name')
        ->toOptionArray();
}