请在下面找到我的代码
module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'ShoppingModelShopTable' => function($sm) {
$tableGateway = $sm->get('ShopTableGateway');
$table = new ShopCategoriesTable($tableGateway);
return $table;
},
'ShopTableGateway' => function ($sm) {
$dbAdapter = $sm->get('ZendDbAdapterAdapter');
$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('ShoppingModelShopTable');
}
return $this->shopTable;
}
你可以看到在我的第一个代码shop_categories
是我的数据库表,我从中获取数据,上面的代码工作得很好。但现在我需要从另一个名为shop_goods
表获取数据,我如何配置模块。php?
试试这个:
module.php
<?php
public function getServiceConfig()
{
return array(
'factories' => array(
'ApplicationModelShopgoodsTable' => function($sm) {
$tableGateway = $sm->get('ShopgoodsTableGateway');
$table = new ShopgoodsTable($tableGateway);
return $table;
},
'ShopgoodsTableGateway' => function ($sm) {
$dbAdapter = $sm->get('ZendDbAdapterAdapter');
$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('ShoppingModelShopgoodsTable');
}
return $this->shopGoodsTable;
}
你必须使用修改你的查询,使用JOIN为你的sql查询,但这将是一个问题,因为你的映射器可能不知道其他表值填充的结果。所以,你有两个选择。1)使用join和Modify映射器——我得说,这不是一个干净的方法2)学会运用教义,它就会处理这样的事情。您正在使用TableGateway。只有在每个映射器执行每个表的事务时才有用。如果你想使用一/一多关系场景,你可能需要像第一点那样欺骗你的代码,这会导致复杂性。所以原则就是解决办法
下面是我如何做到这一点的一个例子。我的模块名称是Application,我从'projects'和'users'中获取数据的两个表。
Module.php
namespace Application;
// Project db
use ApplicationModelProject;
use ApplicationModelProjectTable;
// User db
use ApplicationModelUser;
use ApplicationModelUserTable;
// db connection
use ZendDbResultSetResultSet;
use ZendDbTableGatewayTableGateway;
class Module {
public function onBootstrap(MvcEvent $e) {...}
public function getConfig() {...}
public function getAutoloaderConfig() {...}
public function getServiceConfig() {
return array(
'factories' => array(
'ApplicationModelProjectTable' => function($sm) {
$tableGateway = $sm->get('ProjectTableGateway');
$table = new ProjectTable($tableGateway);
return $table;
},
'ProjectTableGateway' => function ($sm) {
$dbAdapter = $sm->get('ZendDbAdapterAdapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Project());
return new TableGateway('projects', $dbAdapter, null, $resultSetPrototype);
},
/*** Add other table gateways here ***/
'ApplicationModelUserTable' => function($sm) {
$tableGateway = $sm->get('UserTableGateway');
$table = new UserTable($tableGateway);
return $table;
},
'UserTableGateway' => function($sm) {
$dbAdapter = $sm->get('ZendDbAdapterAdapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new User());
return new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
in my controller…
public function indexAction() {
return new ViewModel(array(
'projects' => $this->getProjectTable()->fetchAll(),
'users' => $this->getUserTable()->fetchAll(),
));
}
那么,在shoppingcontroller。php文件中,只要你的控制器类扩展了AbstractActionController并且你包含了。
use ZendMvcControllerAbstractActionController;
use ZendViewModelViewModel;
您应该能够返回一个ViewModel对象,该对象包含从单独的数据库表中获取的数据。然后您可以在视图中随意使用。例如,我在视图中循环遍历$projects和$users以显示内容。