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

时间:2013-11-18 08:56:19

标签: php zend-framework2

我在xampp上使用zend framework 2.2。我有一个模块'相册'。

目录结构:

enter image description here

我的数据库表:'album'

模块代码:

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

}

控制器代码:

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

型号代码:

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

查看代码:

 <?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>

我得到如下图所示的输出。 enter image description here

现在我想从不同的表('abm')获取数据,而'album'表仍将在模块代码中声明。

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

(并假设我的两个数据表具有相同的结构。所以视图页面和控制器将是相同的。) 那我该怎么办呢?如何在不同数据表的模型代码中使用TableGateway()?

-Thanks。

编辑:

我做了以下,

模块:

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

}

控制器:

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

模型(新模型页面:'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;
    }
 }

然后我在视图页面收到'$ abms'并带有'abm'表数据。

更新:

它使用相同的模型获取不同的表。

模块:

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

控制器:

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

型号:

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 个答案:

答案 0 :(得分:1)

如果我理解正确你只需要为另一个表'abm'创建另一个TableGateway并为其创建实体类,那么你可以获取两个表并在视图中显示它们。