doujiao8491 2013-11-18 08:56
浏览 112
已采纳

从各种数据库表中获取数据

I'm using zend framework 2.2 on xampp. I've a module 'Album'.

directory structure :

enter image description here

my database table:'album'

module code :

<?php

namespace Album;

use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module {

    public function getAutoloaderConfig() {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig() {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig() {
        return array(
            'factories' => array(
                'Album\Model\AlbumTable' => function($sm) {
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }

}

controller code :

<?php

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\Album;
use Album\Form\AlbumForm;

class AlbumController extends AbstractActionController {

    protected $albumTable;

    public function getAlbumTable() {
        if (!$this->albumTable) {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }

    public function indexAction() {
        return new ViewModel(array(
                    'albums' => $this->getAlbumTable()->fetchAll(),
                    'active' => 'albumindex',
                ));
}
}

model code :

<?php

namespace Album\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

 class AlbumTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
       $this->tableGateway = $tableGateway;
     }

     public function fetchAll($paginated=false)
     {
           $resultSet =$this->tableGateway->select(function (Select $select) {
          $select->order('title ASC');
          });
            return $resultSet;
    }
}

view code :

 <?php
 // module/Album/view/album/album/index.phtml:

 $title = 'My albums';
 $this->headTitle($title);
 ?>
 <h1><?php echo $this->escapeHtml($title); ?></h1>
 <p>
     <a href="<?php echo $this->url('album', array('action'=>'add'));?>">Add new album</a>
 </p>

 <table class="table">
 <tr>
     <th>Title</th>
     <th>Artist</th>
     <th>&nbsp;</th>
 </tr>
 <?php foreach ($albums as $album) : ?>
 <tr>
     <td><?php echo $this->escapeHtml($album->title);?></td>
     <td><?php echo $this->escapeHtml($album->artist);?></td>
     <td>
         <a href="<?php echo $this->url('album',
             array('action'=>'edit', 'id' => $album->id));?>">Edit</a>
         <a href="<?php echo $this->url('album',
             array('action'=>'delete', 'id' => $album->id));?>">Delete</a>
     </td>
 </tr>
 <?php endforeach; ?>
 </table>

I get the output like the following picture. enter image description here

now I want to fetch data from different table('abm') while 'album' table still will be declared in module code.

  return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);

(and suppose my both data tables have same structure.so view page and controller will be same.) so what should I do for that ? how can I use TableGateway() in model code for different data table ?

-thanks.

Edit:

I did the following,

Module :

<?php

namespace Album;

use Album\Model\Album;
use Album\Model\AlbumTable;
**use Album\Model\AbmTable;**
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module {

    public function getAutoloaderConfig() {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig() {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig() {
        return array(
            'factories' => array(
                'Album\Model\AlbumTable' => function($sm) {
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
                **'Album\Model\AbmTable' => function($sm) {
                    $tableGateway = $sm->get('AbmTableGateway');
                    $table = new AbmTable($tableGateway);
                    return $table;
                },                        
                'AbmTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('abm', $dbAdapter, null, $resultSetPrototype);
                },**                        
            ),
        );
    }

}

Controller :

<?php

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\Album;
use Album\Form\AlbumForm;

class AlbumController extends AbstractActionController {

    protected $albumTable;protected $abmTable;

    public function getAlbumTable() {
        if (!$this->albumTable) {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }

    **public function getAbmTable() {
        if (!$this->abmTable) {
            $sm = $this->getServiceLocator();
            $this->abmTable = $sm->get('Album\Model\AbmTable');
        }
        return $this->abmTable;
    }**    

    public function indexAction() {
        return new ViewModel(array(
                    'albums' => $this->getAlbumTable()->fetchAll(),
                    **'abms' => $this->getAbmTable()->fetchAll(),**
                ));
     }
}

Model (a new model page: 'AbmTable.php') :

<?php

namespace Album\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

 class AbmTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
       $this->tableGateway = $tableGateway;
     }

     public function fetchAll($paginated=false)
     {
           $result =$this->tableGateway->select(function (Select $select) {
          $select->order('title ASC');
          });
            return $result;
    }
 }

then I received '$abms' in view page with 'abm' table data.

Update :

it fetches different tables with same model.

Module :

namespace Album;

use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module {

    public function getAutoloaderConfig() {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig() {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig() {
        return array(
            'factories' => array(
// 'album' table-------------------------------------                 
                'Album\Model\AlbumTable\dbtable=album' => function($sm) {
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
// 'abm' table-------------------------------------                        
                'Album\Model\AlbumTable\dbtable=abm' => function($sm) {
                    $tableGateway = $sm->get('AbmTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },                        
                'AbmTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('abm', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
}

Controller :

namespace Album\Controller;

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

class AlbumController extends AbstractActionController {

    protected $albumTable;
    protected $abmTable;

    public function getAlbumTable() {
        if (!$this->albumTable) {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable\dbtable=album');
        }
        return $this->albumTable;
    }

    public function getAbmTable() {
        if (!$this->abmTable) {
            $sm = $this->getServiceLocator();
            $this->abmTable = $sm->get('Album\Model\AlbumTable\dbtable=abm');
        }
        return $this->abmTable;
    }    

    public function indexAction() {
        return new ViewModel(array(
                    'albums' => $this->getAlbumTable()->fetchAll(),
                    'abms' => $this->getAbmTable()->fetchAll(),
                ));
    }
 }

Model :

namespace Album\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;

 class AlbumTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
       $this->tableGateway = $tableGateway;
     }

     public function fetchAll($paginated=false)
     {
           $result =$this->tableGateway->select(function (Select $select) {
          $select->order('title ASC');
          });
            return $result;
    }
 }
  • 写回答

1条回答 默认 最新

  • douhuan1901 2013-11-18 13:03
    关注

    If i understand you correctly you just have to make another TableGateway for another table 'abm' and make entity class for that then you can fetch both tables and show them in view.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)