Search code examples
phpzend-frameworkzend-formzend-dbzend-studio

Zend - populate combobox with database returned data


I try to do it this way:

public function init()
{
    /* Form Elements & Other Definitions Here ... */

    $sets_table = new Optionals_Model_DbTable_Sets();

    $set = new Zend_Form_Element_Select('set');
    $set ->setLabel('Alegeti setul de optionale:');

    foreach ($sets_table->getSets() as $value) {
        echo $value->cod_set_optional;
        $set->addMultiOption($value->cod_set_optional);
    }

    $submit = new Zend_Form_Element_Submit('Continua');

    $this->addElements ( array (
            $set,
            $submit
    ) );
}

where getSets() from the DbTable looks like this:

public function getSets()
{
    $select = $this->select();
    $rows = $this->fetchAll($select);
    if (!$rows) {
        throw new Exception("Could not find!");
    }
    return $rows;
}

I've seen this kind of doing it HERE, but it doesn't work. The echo works fine, but the combobox is not populated. Is there something wrong in my code or what?

Thank you! Sorin


Solution

  • addMultiOption($option, $value) requires two parameters and you are giving it only a value.

    you should do this :

     foreach ($sets_table->getSets() as $set) {
            $set->addMultiOption($set->id , $set->value);
        }
    

    make sure your getSets returns both an id and a value from your table.