如何在zf2中从数据库加载选择选项

时间:2016-01-16 10:02:49

标签: php zend-framework zend-framework2 zend-form

我一直关注blog的zf2指南我创建了控制器,工厂,表格,映射器,模型,服务,视图等所有内容

在我的表单中,我有一个选择元素

$this->add(array(     
  'type' => 'select',       
  'name' => 'roleId',
  'attributes' =>  array(
    'id' => 'roleId',
    'options' => array(
        '1' => 'Admin',
        '2' => 'Manager',
    ),
  ),
  'options' => array(
    'label' => 'Role',
  ),
));

现在在这种形式中,我想从数据库加载角色的选项 我尝试通过创建一个简单的函数来加载该选项,该函数可以在元素中访问,如下所示,但我无法获取结果。我已经创建了Controller,Factory,Form,Mapper,Model,Service和view,我可以在Role上进行CRUD操作。

$this->add(array(     
  'type' => 'select',       
  'name' => 'roleId',
  'attributes' =>  array(
    'id' => 'roleId',
    'options' => $this->getAllRoles(),
  ),
  'options' => array(
    'label' => 'Role',
  ),
));

public function getAllRoles()
{
  $roles = $this->getServiceLocator()->get('Admin\Service\RoleService');
  $allRoles = $this->getAllTheRoles();  
  return $allroles;
}

任何人都可以指导我如何加载Blog Post后面的IndexAction中列出的所有角色选项,其中包含角色的ID和名称。

3 个答案:

答案 0 :(得分:0)

您可以创建一个预先填充了角色的可重用表单元素。为此,您必须使用 data <- structure(list(doy = c(264, 265, 266, 267, 268, 271, 272, 11,12, 13), Date = structure(c(1442824200, 1442910600, 1442997000,1443083400, 1443169800, 1443429000, 1443515400, 1452504600, 1452591000,1452677400), class = c("POSIXct", "POSIXt"), tzone = ""), OR_High = c(164.71,162.96, 163.38, 161.37, 163.91, 162.06, 160.22, 164.5, 165.23,165.84), OR_Low = c(164.37, 162.62, 162.98, 161.06, 163.57, 161.66,159.7, 164.06, 164.84, 165.4), HOD = c(165.56, 163.36, 163.38,162.24, 164.43, 162.06, 160.96, 164.5, 165.78, 165.84), LOD = c(165.22,163.1, 162.98, 161.95, 164.24, 161.66, 160.75, 164.06, 165.56,165.4), Close = c(164.92, 163.02, 162.58, 161.85, 162.94, 159.84,160.19, 163.83, 165.02, 161.38), Range = c(0.340000000000003,0.260000000000019, 0.400000000000006, 0.29000000000002, 0.189999999999998,0.400000000000006, 0.210000000000008, 0.439999999999998, 0.219999999999999,0.439999999999998), `A-val` = c(NA, NA, NA, NA, NA, NA, NA, 0.0673439999999994,0.0659639999999996, 0.0729499999999996), `A-up` = c(NA, NA, NA,NA, NA, NA, NA, 164.567344, 165.295964, 165.91295), `A-down` = c(NA,NA, NA, NA, NA, NA, NA, 163.992656, 164.774036, 165.32705)), .Names = c("doy","Date", "OR_High", "OR_Low", "HOD", "LOD", "Close", "Range","A-val", "A-up", "A-down"), row.names = c(1L, 2L, 3L, 4L, 5L,6L, 7L, 78L, 79L, 80L), class = "data.frame") 中的表单元素管理器注册该服务。

module.config.php

不需要扩展标准选择类,因为更改只是配置。这最好在工厂类中完成。

return [
    'form_elements' => [
        'factories' => [
            'RoleSelect' => 'MyModule\Form\Element\RoleSelectFactory',
        ],
    ],
];

然后可以更新在表单中添加元素以使用我们注册的服务的名称。

namespace MyModule\Form\Element;

use Zend\Form\Element\Select;

class RoleSelectFactory
{
    public function __invoke($formElementManager, $name, $rname)
    {
        $select = new Select('role_id');

        $select->setOptions(['label' => 'Role']);
        $select->setAttributes(['id' => 'role_id']);

        $serviceManager = $formElementManager->getServiceLocator();
        $roleService = $serviceManager->get('Admin\Service\RoleService');

        $options = [];
        foreach($roleService->getAllTheRoles() as $role){
            $options[$role->getId()] => $role->getName();
        }

        $select->setValueOptions($options);

        return $select;
    }
}

要记住的一点是,必须使用$this->add([ 'name' => 'role_id' 'type' => 'RoleSelect', ]); 创建使用此元素的表单

答案 1 :(得分:-1)

终于找到了这样做的简单方法,我真的不确定这是否是正确的方法。

在用户控制器中添加了角色服务

我的userController.php中的代码

use Admin\Service\RoleServiceInterface;
class UserController extends AbstractActionController
{
  /**
  * @var \Admin\Service\UserServiceInterface
  */
  protected $userService;
  protected $roleService;
  protected $userForm;

  public function __construct(
    UserServiceInterface $userService,
    RoleServiceInterface $roleService,
    FormInterface $userForm
  )
  {
    $this->userService = $userService;
    $this->roleService = $roleService;
    $this->userForm    = $userForm;
  }

  public function addAction()
  {
    $request = $this->getRequest();
    $roles    = $this->roleService->findAllRoles();
    foreach ($roles as $role) {
      if($role->getStatus() == 1) {
        $allRoles[$role->getId()] = $role->getName();
      }
    }
    $this->userForm->__construct(null, array('roleOptions'=>$allRoles));
  }
}

我的UserControllerFactory.php

<?php

namespace Admin\Factory;

use Admin\Controller\UserController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class UserControllerFactory implements FactoryInterface
{
  /**
  * Create service
  *
  * @param ServiceLocatorInterface $serviceLocator
  *
  * @return mixed
  */
  public function createService(ServiceLocatorInterface $serviceLocator)
  {
    $realServiceLocator = $serviceLocator->getServiceLocator();
    $userService        = $realServiceLocator->get('Admin\Service\UserServiceInterface');
    $roleService        = $realServiceLocator->get('Admin\Service\RoleServiceInterface');
    $userInsertForm     = $realServiceLocator->get('FormElementManager')->get('Admin\Form\UserForm');

    return new UserController(
    $userService,
    $roleService,
    $userInsertForm
    );
  }
}

最后是UserForm.php

<?php

namespace Admin\Form;

use Zend\Form\Form;
use Admin\Model\User;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Form\Element\Select;


class UserForm extends Form
{
  public function __construct($name = null, $options = array())
  {
    $roleOptions = array();
    if($options) {
      $roleOptions = $options['roleOptions'];
    }
    parent::__construct($name, $options);

    $this->setHydrator(new ClassMethods(false));
    $this->setObject(new User());
    $this->add(array(
      'type' => 'hidden',
      'name' => 'id'
    ));

   $this->add(array(     
     'type' => 'select',       
     'name' => 'roleId',
     'attributes' =>  array(
       'id' => 'roleId',
       'options' => $roleOptions
     ),
    'options' => array(
      'label' => 'Role',
    ),
    ));
  }
}

这种方式使用服务管理器我已成功加载my for Select选项。

答案 2 :(得分:-1)

在控制器内部调用getAllRoles()然后您可以在创建form对象时将自定义数组作为form的参数传递。在form __construct函数中,您可以检索该数组并像这样设置

  

'options'=&gt; $角色,