2013-06-05 34 views
1

请在下面找到Zend公司:从多个表中获取数据

module.php

public function getServiceConfig() 
{ 
return array(
'factories' => array(
'Shopping\Model\ShopTable' => function($sm) { 
    $tableGateway = $sm->get('ShopTableGateway'); 
    $table = new ShopCategoriesTable($tableGateway); 
    return $table; 
}, 
'ShopTableGateway' => function ($sm) { 
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
    $resultSetPrototype = new ResultSet(); 
    $resultSetPrototype->setArrayObjectPrototype(new ShopCategories()); 
    return new TableGateway('shop_goods', $dbAdapter, null, $resultSetPrototype); 
}, 
), 
); 
} 

shoppingcontroller.php

public function getShopTable() 
{ 
     if (!$this->shopTable) 
     { 
      $sm = $this->getServiceLocator(); 
      $this->shopTable = $sm->get('Shopping\Model\ShopTable'); 
     } 
     return $this->shopTable; 
} 

我的代码,你可以在我的第一个代码见shop_categories是我的数据库表,从中获取数据的iam,上面的代码工作正常。但现在我需要从其他表中取数据命名为shop_goods如何配置module.php?

回答

1

试试这个:

module.php

<?php 
    public function getServiceConfig() 
    { 
     return array(
      'factories' => array(
       'Application\Model\ShopgoodsTable' => function($sm) { 
        $tableGateway = $sm->get('ShopgoodsTableGateway'); 
        $table = new ShopgoodsTable($tableGateway); 
        return $table; 
       }, 
       'ShopgoodsTableGateway' => function ($sm) { 
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
        $resultSetPrototype = new ResultSet(); 
        $resultSetPrototype->setArrayObjectPrototype(new Shopgoods()); 
        return new TableGateway('shops_goods', $dbAdapter, null, $resultSetPrototype); 
       }, 
      ), 
     ); 
    } 

而在你的控制器

public function getShopgoodsTable() 
{ 
     if (!$this->shopGoodsTable) 
     { 
      $sm = $this->getServiceLocator(); 
      $this->shopGoodsTable= $sm->get('Shopping\Model\ShopgoodsTable'); 
     } 
     return $this->shopGoodsTable; 
} 
+0

Hi duques,我的意思是说,我必须同时从两个表中提取数据... – Friend

+0

在一个请求中? – Tounu

+0

我想,加入查询? – Pierre

0

那么你必须使用修改查询,使用JOIN为您的SQL查询,但这样会是一个问题,因为您的映射器可能不知道其他表值将填充结果。所以,你有两个选择。 1)使用连接和修改你的映射器 - 不是一个干净的方法,我会说 2)学习使用原则,它会处理这样的事情。您正在使用TableGateway。只有在您为每个映射器执行每个表的事务时才是好事。如果你想使用一个/一个多的关系场景,你可能不得不像第1点那样欺骗你的代码,这会导致并发症。因此,学说是解决方案

0

这是我如何完成这个的一个例子。我的模块名称是Application,我从中获取数据的两个表格是'项目'和'用户'。

Module.php

namespace Application; 

// Project db 
use Application\Model\Project; 
use Application\Model\ProjectTable; 

// User db 
use Application\Model\User; 
use Application\Model\UserTable; 

// db connection 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 

class Module { 

public function onBootstrap(MvcEvent $e) {...} 
public function getConfig() {...} 
public function getAutoloaderConfig() {...} 

public function getServiceConfig() { 
    return array(
     'factories' => array(
      'Application\Model\ProjectTable' => function($sm) { 
       $tableGateway = $sm->get('ProjectTableGateway'); 
       $table = new ProjectTable($tableGateway); 
       return $table; 
      }, 
      'ProjectTableGateway' => function ($sm) { 
       $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
       $resultSetPrototype = new ResultSet(); 
       $resultSetPrototype->setArrayObjectPrototype(new Project()); 
       return new TableGateway('projects', $dbAdapter, null, $resultSetPrototype); 
      }, 

      /*** Add other table gateways here ***/ 
      'Application\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('users', $dbAdapter, null, $resultSetPrototype); 
      }, 
     ), 
    ); 
} 
} 
在我的控制器

...

public function indexAction() { 
    return new ViewModel(array(
     'projects' => $this->getProjectTable()->fetchAll(), 
     'users' => $this->getUserTable()->fetchAll(), 
    )); 
} 

因此,在你shoppingcontroller.php文件,只要你的控制器类扩展AbstractActionController,并在其中...

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

你应该能够返回一个ViewModel对象,它包含从单独的数据库表中获取的数据。然后你可以在你的视图中使用。例如,我循环浏览我的视图中的$ projects和$ users以显示内容。