0

我有我的应用程序基于 ZendSkeletonApplication ,现在我想创建我的模型之间的关系,所以:

user            portal
id              id
firstName       name
lastName        url
........
portal_Id

我想用数据库值填充用户表单中的选择选项

<?php
namespace Register\Form;

use Zend\Captcha\AdapterInterface as CaptchaAdapter;
use Zend\Form\Form;
use Zend\Form\Element;


class UserForm extends Form
{
    protected $portalTable;

    public function __construct($name = null)
    {
        parent::__construct('user');
        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'form-horizontal');

        $this->add(array(
                'name' => 'id',
                'attributes' => array(
                        'type'  => 'hidden',
                ),
        ));

        $this->add(array(
                'type' => 'Select',
                'name' => 'portal_id',
                'options' => array(
                        'label' => 'Portal',
                        'empty_option' => 'Seleccione un portal',
                        'value_options' => array(
                                '1' => 'portal 1',
                                '2' => 'portal 2',
                                '3' => 'portal 3',
                                //i want option from database with 
                        ),

                )
        ));

        $this->add(array(
                'name' => 'firstName',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'First Name',
                ),
        ));

        $this->add(array(
                'name' => 'lastName',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'Last Name',
                ),
        ));
        $this->add(array(
                'name' => 'login',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'Login',
                ),
        ));

        $this->add(array(
                'name' => 'password',
                'attributes' => array(
                        'type'  => 'password',
                ),
                'options' => array(
                        'label' => 'Password',
                ),
        ));

        $this->add(array(
                'name' => 'password_repeat',
                'attributes' => array(
                        'type'  => 'password',
                ),
                'options' => array(
                        'label' => 'password (repeat)',
                ),
        ));

        $this->add(array(
                'name' => 'email',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'Email',
                ),
        ));

        $this->add(array(
                'type' => 'Zend\Form\Element\Csrf',
                'name' => 'csrf',
                'options' => array(
                        'csrf_options' => array(
                                'timeout' => 600
                        )
                )
        ));

        $this->add( array(
                'type' => 'Captcha',
                'name' => 'captcha',
                'options' => array(
                        'label' => 'Please verify you are human.',
                        'captcha' => array('class' => 'Dumb',
                        ),
                ),
        ));

        $this->add(array(
                'name' => 'submit',
                'attributes' => array(
                        'type'  => 'submit',
                        'value' => 'Go',
                        'id' => 'submitbutton',
                ),
        ));
    }
}

在这部分我想从数据库中填充选择

$this->add(array(
                'type' => 'Select',
                'name' => 'portal_id',
                'options' => array(
                        'label' => 'Portal',
                        'empty_option' => 'Seleccione un portal',
                        'value_options' => array(
                                '1' => 'portal 1',
                                '2' => 'portal 2',
                                '3' => 'portal 3',
                                //i want option from database with 
                        ),

                )
        ));

对不起我的英语不好

4

2 回答 2

1

我已经写了一篇关于这个主题的深入博客“ Zend\Form\Element\Select and Database-Values ”。基本上,这是你必须做的:

基本上,您所要做的就是在表单中查询数据库以获取数据。为此,您需要在表单中提供 DB-Adapter,这是由 Dependency-Injection 完成的。由于 DB-Adapter 是required为了让您的表单正常运行,我建议使用 Setter-Injection。

在你里面getServiceConfig()这样做:

return array('factories' => array(
    'namespace-form-formname' => function($sm) {
        $dbA  = $sm->get('Zend\Db\Adapter\Adapter');
        $form = new \Namespace\Form\Formname($dbA);

        return $form;
    }
));

这会将 注入Zend\Db\Adapter\Adapter到您的表单中,该表单应该已经通过其他配置有效。然后你需要稍微修改你的表单代码:

public function __construct(\Zend\Db\Adapter\Adapter $dbA) {
    parent::__construct('form-name');

    // Do the DB-Query here. You got the DB-Adapter
    // http://zf2.readthedocs.org/en/latest/modules/zend.db.adapter.html
    $selectArray =  array(
        'key' => 'value',
        'key' => 'value',
        'key' => 'value',
    ); // obviously, this is just a fake-$selectArray demonstrating 
       // what the output of your Queries should be

    // Add your Form Elements here
    // use $selectArray as value_options of your desired select element
}

基本上就是这样。遗憾的是我无法给你一个具体的例子,因为我从来没有使用过Zend\Db,但我认为这会让你开始。

PS:在您的控制器中,像这样调用表单:

$form = $this->getServiceLocator()->get('namespace-form-formname');
于 2013-03-20T16:42:37.793 回答
0
Try:
// add code on controller
$arrPortalId = array();
$results = array('1' => 'portal 1', '2' => 'portal 2', '3' => 'portal 3',); // this part change your database value
foreach ($results as $key => $val) {
$arrPortalId[$key] = $va;
}
$dataParams['portalId'] = $arrPortalId;
$form = new UserForm($dataParams);

 <?php
namespace Register\Form;

use Zend\Captcha\AdapterInterface as CaptchaAdapter;
use Zend\Form\Form;
use Zend\Form\Element;


class UserForm extends Form
{
    protected $portalTable;

    public function __construct($params = array())
    {   $name = isset($params['name'])?$params['name']:'';
        parent::__construct('user');
        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'form-horizontal');

        $this->add(array(
                'name' => 'id',
                'attributes' => array(
                        'type'  => 'hidden',
                ),
        ));
    $portalId = (isset($params['portalId']) && count($params['portalId']) > 0)?$params['portalId']:array();
        $this->add(array(
                'type' => 'Select',
                'name' => 'portal_id',
                'options' => array(
                        'label' => 'Portal',
                        'empty_option' => 'Seleccione un portal',
                        'value_options' => $portalId,

                )
        ));

        $this->add(array(
                'name' => 'firstName',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'First Name',
                ),
        ));

        $this->add(array(
                'name' => 'lastName',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'Last Name',
                ),
        ));
        $this->add(array(
                'name' => 'login',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'Login',
                ),
        ));

        $this->add(array(
                'name' => 'password',
                'attributes' => array(
                        'type'  => 'password',
                ),
                'options' => array(
                        'label' => 'Password',
                ),
        ));

        $this->add(array(
                'name' => 'password_repeat',
                'attributes' => array(
                        'type'  => 'password',
                ),
                'options' => array(
                        'label' => 'password (repeat)',
                ),
        ));

        $this->add(array(
                'name' => 'email',
                'attributes' => array(
                        'type'  => 'text',
                ),
                'options' => array(
                        'label' => 'Email',
                ),
        ));

        $this->add(array(
                'type' => 'Zend\Form\Element\Csrf',
                'name' => 'csrf',
                'options' => array(
                        'csrf_options' => array(
                                'timeout' => 600
                        )
                )
        ));

        $this->add( array(
                'type' => 'Captcha',
                'name' => 'captcha',
                'options' => array(
                        'label' => 'Please verify you are human.',
                        'captcha' => array('class' => 'Dumb',
                        ),
                ),
        ));

        $this->add(array(
                'name' => 'submit',
                'attributes' => array(
                        'type'  => 'submit',
                        'value' => 'Go',
                        'id' => 'submitbutton',
                ),
        ));
    }
}
于 2015-10-24T10:40:11.413 回答