Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers

up vote 1 down vote accepted

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;
}
share|improve this answer
Hi duques , I mean to say i have to fetch data from both the tables at the same time... – Friend Jun 5 at 9:25
Within a single request ? – Tounu Jun 5 at 9:28
I think, Join query ? – duques_l Jun 5 at 9:47

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.