0

我实际上在做的是,从数据库中获取公司列表并将其传递给表单 SELECT 元素。所以我创建了一个模型文件,它返回一个数组

//=== return an array of $ID => $name of companies to use in dropdown in reports form
public function getTotalResult($table, $type, $id) {
        $this->table = $table;
        $select = new Select();
        $spec = new  Where();
        $spec->equalTo('status', 1);
        if ($type == 'name') {
            $spec->equalTo('id', $id);
        }
        $select->from($this->table);

        $select->where($spec);
        $resultSet = $this->selectWith($select);
        //$resultSet->buffer();
        return $resultSet;

}
public function resultList($table){
    $results = $this->getTotalResult($table, '', '');
    foreach ($results as $result) {
        $this->id[] = $result->id;
        $this->name[] = $result->name;
    }
    $result = array_combine($this->id, $this->name);
    return $result;

}

然后我在我的控制器中对此进行了测试,它返回了我想要的结果:

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use SpangelLogin\Model\Register;          // <-- Add this import
use SpangelLogin\Model\companyList;          // <-- Add this import
class RegisterController extends AbstractActionController
{
protected $registerTable;
protected $companyList;

 public function getcompanyList()
{
    if (!$this->companyList) {
        $sm = $this->getServiceLocator();
        $this->companyList = $sm->get('SpangelLogin\Model\companyList');
    }
    return $this->companyList;
}

public function indexAction()
{
    //== get list of companies
    $company_table = 'rs_company';
    $sector_table = 'rs_sector';
    $companiesList = $this->getcompanyList()->getName($company_table, 2);

 }
}

所以现在我想在我的表单的 Select Element 中传递这个 CompaniesList 数组。我怎样才能做到这一点。这是我使用 select 的表单。

use Zend\Form\Form;

使用 Zend\Form\Element;

类 SectorReportForm 扩展表单 {

public function __construct($name = null)
{
    // we want to ignore the name passed
    parent::__construct('sectorreport');
    $companiesArray =  $this->companiesList();
    $sectorsArray =  $this->sectorsList();

    $this->setAttribute('method', 'post');
    $this->setAttribute('enctype','multipart/form-data');

    $this->add(array(     
        'type' => 'Zend\Form\Element\Select',       
        'name' => 'company',
        'attributes' =>  array(
            'id' => 'company',  
            'multiple' => true,               
            'options' => $companiesArray,
        ),
        'options' => array(
            'label' => 'Company',
        ),
    ));  


    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Upload',
            'id' => 'submitbutton',
            'class' => 'button violet right'
        ),
    ));
}


}
4

1 回答 1

1

从设计的角度来看,最好的方法是通过依赖注入来处理这个问题。那个鬼鬼祟祟的小流行语让人们如此困惑,但实际上只不过是在对象之间转发数据:P

可以在以下答案以及我的博客文章中看到表单的一般依赖注入

如果您不想采用这种方法,也可以在 Controller 级别进行处理。

$form   = new My\Form();
$select = $form->get('selectCountries');

$model    = new My\Countries();
$listData = $model->getCountriesAsArray();

$select->setValueOptions($listData);

我仍然建议您采用不同的方法 ;) 保持控制器更干净,这总是一件好事。分离关注!

于 2013-04-19T07:29:15.980 回答