php - ZF2 : how to get options for dropdown from db

标签 php zend-framework2

我关注了 tutorial .但是我找不到像这样从数据库中选择填充表单的方法:

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

有没有人有什么想法?

最佳答案

我使用一个函数来检索数据并将其设置为以下形式:

来 self 的工厂:

    $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;
}

关于php - ZF2 : how to get options for dropdown from db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12341114/

相关文章:

php - 从 PHP Zend 2 中的 POST 请求中解码 JSON 字符串

php - 无法从命令行访问 phpmyadmin

php - ZF2 服务管理器

php - Zend Framework 2 - 用于教义和注释表单的 Magic Getter 和 Setter

php - 如何在Symfony2中捕获ParamConverter异常?

php - MySQL 通过代理 php 连接

php - Laravel,添加门以查看用户是否有权访问

php - 使用外部文件元素更改类

php - XAMPP 可用于具有 Windows 操作系统的生产服务器

php - 在 ZF2 中使用表单注释和 DoctrineORM 在 composedObject 中呈现值