0

How would You populate Zend_Form_Element_Select with options direct from Zend_Db_Table_Row?

For instance:

    $select = new Zend_Form_Element_Select('user_id', array(
        'required'  => true
        ));

    // fetching users for select
    $userTable = new User_Model_DbTable_User();
    $users = $userTable->fetchAll();
    $select->addMultiOptions($users->toArray());

But this will not work to good. Let say I want to have object id as a option value and some object property as an select label.

I know I can run foreach thourgh the rowset and construct an array of options but maybe there is some kind of map function?

2 Answers 2

2

Any map function you create would be iterating the rowset so you might as well simply do that, eg

foreach ($users as $user) {
    $select->addMultiOption($user->id, $user->someObjectProperty);
}
Sign up to request clarification or add additional context in comments.

Comments

2

you also may consider tailoring a model method to return exactly the array you want use. Perhaps something similar to:

public function fetchSelectList() {
        $resultSet = $this->fetchAll();
        $entries = array();
        foreach ($resultSet as $row) {
            $entry['id'] = $row->id;
            $entry['name'] = $row->name;

            $entries[] = $entry;
        }
        return $entries;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.