2

我有一个问题。我不知道如何在 zf2 中使用多表和连接查询。

我正在为模型使用服务管理器和 TableGateway。但在这方面它总是使用单表。如何在单个控制器或模型中使用多个表?

在这里,我使用Module.php文件并配置表网关。当我添加另一个表网关时,它会给我错误。

public function getServiceConfig()
 {
     return array(
         'factories' => array(
             'User\Model\UserTable' =>  function($sm) {
                 $tableGateway = $sm->get('UserTableGateway');
                 $table = new UserTable($tableGateway);
                 return $table;
             },
             'UserTableGateway' => function ($sm) {
                 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 $resultSetPrototype = new ResultSet();
                 $resultSetPrototype->setArrayObjectPrototype(new User());
                 return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
             },
         ),
     );
 }

我需要userItems通过UseritemTable.phpUser/Model/UseritemTable. 我怎样才能做到这一点?

Error message
Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getUseritemTable

更新了 Module.php

public function getServiceConfig()
     {
         return array(
             'factories' => array(
                 'User\Model\UserTable' =>  function($sm) {
                     $tableGateway = $sm->get('UserTableGateway');
                     $table = new UserTable($tableGateway);
                     return $table;
                 },
                 'UserTableGateway' => function ($sm) {
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                     $resultSetPrototype = new ResultSet();
                     $resultSetPrototype->setArrayObjectPrototype(new User());
                     return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
                 },
                 'User\Model\UseritemTable' =>  function($sm) {
                     $tableGateway = $sm->get('UseritemTableGateway');
                     $table = new UseritemTable($tableGateway);
                     return $table;
                 },
                 'UseritemTableGateway' => function ($sm) {
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                     $resultSetPrototype = new ResultSet();
                     $resultSetPrototype->setArrayObjectPrototype(new Useritem());
                     return new TableGateway('useritem', $dbAdapter, null, $resultSetPrototype);
                 },
             ),
         );
     }

此 userItemAction 下的 UserController.php

public function userItemAction()
     {


        $form->get('submit')->setValue('Add');
        $request = $this->getRequest();

         if ($request->isPost()) {
            $item = new Useritem();
            $data = $request->getPost()->toArray();  
            $form->setData($data);
            if($form->isValid()){
                 $item->exchangeArray($form->getData());
                 $this->getUseritemTable()->saveItem($item); // while saveItem cause error using getUseritemTable()


            }

         }

        return array('form' => $form);

     }

这是我的 User\Model\UseritemTable.php 代码

namespace User\Model;

 use Zend\Db\TableGateway\TableGateway;

 class UseritemTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
         $this->tableGateway = $tableGateway;
     }

     public function fetchAll()
     {
         $resultSet = $this->tableGateway->select();
         return $resultSet;
     }


     public function saveItem(User $item)
     {
         $this->tableGateway->insert($item);
     }


 }
4

0 回答 0