1

请在下面找到我的代码

模块.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);
},
),
);
}

购物控制器.php

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

正如您在我的第一个代码中看到的那样,我shop_categories正在从中获取数据的数据库表,上面的代码工作正常。但是现在我需要从另一个名为shop_goods我如何配置 module.php 的表中获取数据?

4

3 回答 3

1

尝试这个 :

模块.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;
}
于 2013-06-05T08:11:34.160 回答
0

Well you have to use modify your query, use JOIN for your sql query but this will be a problem as you mapper might not know other table values to be populated with results. So, you have two options. 1) Use join and Modify your mapper -- not a clean approach, i will say 2) Learn to use doctrine and it will handle such things. You are using TableGateway. It is good only if you are doing transaction per table per mapper. If you want to use one/one-many relationship scenario you might have to trick your code just like in point 1 which will lead in complications. So Doctrine is the solution

于 2013-06-07T05:37:02.787 回答
0

这是我如何完成此操作的示例。我的模块名称是应用程序,我从中获取数据的两个表是“项目”和“用户”。

模块.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 以显示内容。

于 2015-03-01T02:12:07.157 回答