Zend framework2 在zend2中动态设置SELECT属性
我实际上在做的是,从数据库中获取一个公司列表,并将其传递给表单选择元素。 所以我创建了一个模型文件,它返回一个数组Zend framework2 在zend2中动态设置SELECT属性,zend-framework2,Zend Framework2,我实际上在做的是,从数据库中获取一个公司列表,并将其传递给表单选择元素。 所以我创建了一个模型文件,它返回一个数组 //=== 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
//=== 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);
}
}
使用Zend\Form\Element
类SectorReportForm扩展了表单
{
从设计的角度来看,最好的方法是通过依赖注入来处理这个问题 查看以下答案以及我的博客文章,可以看到表单的一般依赖注入
$form = new My\Form();
$select = $form->get('selectCountries');
$model = new My\Countries();
$listData = $model->getCountriesAsArray();
$select->setValueOptions($listData);
我仍然建议您采用不同的方法;)保持控制器更干净,这始终是一件好事。分离关注!请查看我提供的答案。此外,我的链接博客也可能对您有所帮助。总之,请至少查看第一页的问题和答案。这就是我的问题在第一页上已经被问了4次。谢谢你回复Sam。我为重复而道歉。但是我没有得到正确的答案。我已经阅读了你的教程页面。它们很有帮助,但在这种情况下1。没有原则2。没有表网关,因为正在使用多个表。任何可能性,我可以通过控制器或直接建模?很有魅力。谢谢Sam。对于正确的方法,是的,我肯定会改变我的方法。因为当时很多东西对我来说都是新的,而且时间紧迫。对于开发人员来说,永远没有足够的时间来了解我们想要什么,是吗?:D很高兴它奏效。我接受了你的话(普遍真理)请认真阅读您的博客。为此尝试了DbaAdaptor方法,雅皮士..成功了。并解决了我关于Zend Sql查询的另一个问题。谢谢
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'
),
));
}
}
$form = new My\Form();
$select = $form->get('selectCountries');
$model = new My\Countries();
$listData = $model->getCountriesAsArray();
$select->setValueOptions($listData);