Question

Please find my code below

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;
}

As you can see on my first code shop_categories is my database table from which iam fetching data ,above code works fine.But now i need to fetch data from an other table named as shop_goods how do i configure module.php?

Was it helpful?

Solution

Try this :

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

And in your controller

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

OTHER TIPS

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

Here is an example of how I accomplished this. My module name is Application and the two tables I'm fetching data from are 'projects' and 'users'.

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

in my controller...

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

So, in your shoppingcontroller.php file, as long as your controller class is extending AbstractActionController and you've included...

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

you should be able to return a ViewModel object that contains the data fetched from your separate database tables. Then you can use as you'd like in your view. For example, I loop through $projects and $users in my view to display the contents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top