ZF2在单个模块中使用多个TableGatway

时间:2015-04-28 11:19:36

标签: php zend-framework2 tablegateway

我有一个问题。我不知道如何在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.php在用户控制器中插入User/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);
     }


 }

0 个答案:

没有答案