0

我正在尝试以 zend 形式获取我的类别模型,以便使用 zend 框架 2 处理选择元素。

经过大量代码搜索后,我发现我可以注入或提取依赖项。

遵循我在 module.php 中所做的代码

我想要 CategoryForm.php 中的 categoryTable.php(model) 文件

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Category\Model\CategoryTable' =>  function($sm) {
                $tableGateway = $sm->get('CategoryTableGateway');
                $table = new CategoryTable($tableGateway);
                //echo "<pre>";print_r($table);echo "</pre>";
                return $table;
            },
            'CategoryTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Category());
                return new TableGateway('Of_Restaurants_Category', $dbAdapter, null, $resultSetPrototype);
            },
            'Category\Form\CategoryForm' => function ($sm) {
                $service = $sm->get('Category\Model\CategoryTable');
                $form    = new Form;
                $form->setService($service);
                return $form;
            }
        ),
    );
}

然后我将以下代码放入我的控制器中。

 $form = $this->getServiceLocator()->get("Category\Form\CategoryForm");

然后我将以下代码放入我的 CategoryForm.php

public function getCategoryTable()
{
    if (!$this->categoryTable) {

        $sm = $this->getServiceLocator();

        $this->categoryTable = $sm->get('Category\Model\CategoryTable');
    }
    return $this->categoryTable;
}

然后我像这样在同一个文件中调用它

public function __construct($name = null)
{

   parent::__construct('category');
   echo "<pre>";print_r($this->getCategoryTable());die;

   .... other code

我发现了这个错误

Fatal error: Call to undefined method Category\Form\CategoryForm::getServiceLocator() in D:\wamp\www\zendapp\module\Category\src\Category\Form\CategoryForm.php on line 120

请帮忙。我错过了什么吗?

4

1 回答 1

2

我找到了解决方案

第1步

这是我的 module.php 代码

public function getServiceConfig()
{
    return array(
        'invokables' => array(

            'Category\Form\CategoryForm' => 'Category\Form\CategoryForm',

        ),
        'factories' => array(
            'Category\Model\CategoryTable' =>  function($sm) {
                $tableGateway = $sm->get('CategoryTableGateway');
                $table = new CategoryTable($tableGateway);
                //echo "<pre>";print_r($table);echo "</pre>";
                return $table;
            },
            'CategoryTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Category());
                return new TableGateway('Of_Restaurants_Category', $dbAdapter, null, $resultSetPrototype);
            },
        ),
    );
}

第2步

然后在控制器中我做了这个改变

  // $form = new CategoryForm();
  // Service locator now injected
  $form = $this->getServiceLocator()->get('Category\Form\CategoryForm');

步骤:3

然后在我的 categoryForm.php 中我做了以下更改

 use Zend\ServiceManager\ServiceManager;
 use Zend\ServiceManager\ServiceManagerAwareInterface;

受保护的 $serviceManager;

public function getCategoryTable()
{
    if (!$this->categoryTable) {
        $sm = $this->getServiceManager();
        $this->categoryTable = $sm->get('Category\Model\CategoryTable');
    }
    return $this->categoryTable;
}

protected function getCatList()
{

    $groups = $this->getCategoryTable()->fetchAll();
    return $groups;
}

public function getServiceManager()
{
    if ( is_null($this->serviceManager) ) {
        throw new Exception('The ServiceManager has not been set.');
    }

    return $this->serviceManager;
}

public function setServiceManager(ServiceManager $serviceManager)
{
    $this->serviceManager = $serviceManager;
    // Call the init function of the form once the service manager is set
    $this->init();
    return $this;
} 
public function __construct($name = null) // constructor I finished immediately
{
    parent::__construct('category');
}

我添加 INIT() 函数来获取 servicemanager

public function init()
{
   $this->setAttribute('method', 'post');
   $options = array();
    foreach ($this->getCatList() as $cat) {
        $options[$cat->id] = $cat->title;
    }

    $this->add(array(
         'type' => 'Zend\Form\Element\Select',
         'name' => 'parent_id',
         'options' => array(
             'label' => 'Parent Category',
             'empty_option' => 'Please choose Parent Category',
              'value_options' => $options,
         ),

    ));
}

希望这会帮助谁是新的ZF2。

于 2012-11-29T04:47:36.930 回答