3

我按照教程。但是我找不到像这样从数据库中填充表单选择的方法:

// Blog/src/Blog/Form/BlogItemForm.php
$blogCategoryTable = new Model\BlogCategoryTable;
$this->add(new Element\Select('category_id',
    array('label' => 'Category', 'value_options' => $blogCategoryTable->getFormChoices())
    ));

有没有人有任何想法?

4

1 回答 1

5

我使用一个函数来检索数据并将其设置为表单:

从我的工厂:

    $option_for_select = $this->model->getWhatEver();
    $this->add($factory->createElement(array(
        'name' => 'what_ever',
        'type' => 'Zend\Form\Element\Select',
        'attributes' => array(
            'options' => $option_for_select,
        ),
        'options' => array(
            'label' => 'What ever:',
        ),
    )));

从模型:

public function getWhatEver()
{
    $sql = "SELECT something";
    $statement = $this->adapter->query($sql);
    $res =  $statement->execute();

    // set the first option
    $rows[0] = array (
        'value' => '0',
        'label' => 'Top',
        'selected' => TRUE,
        'disabled' => FALSE
    );

    foreach ($res as $row) {
        $rows[$row['triplet_id']] = array (
            'value' => $row['col1'],
            'label' => $row['col2'],
        );
    }
    return $rows;
}
于 2012-09-10T08:26:47.003 回答