Zend Framework 2 的多个表 [英] multiple tables for Zend Framework 2

查看:30
本文介绍了Zend Framework 2 的多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Zend Framework 2 的新手.我成功完成了 ZF2 的相册教程.现在我只想显示数据库中多个表中的某些数据.我有一个带有表的简单数据库设置,例如,人员、书籍、状态等.数据库应该做什么真的不重要.我想知道的是,是否有一个教程可以向我展示显示表连接数据的分步指南.我已经看到一些代码片段显示了如何进行连接,但我还没有找到任何关于设置类以及如何配置 Module.php 的教程.换句话说,Album 中的 Module 在 getServiceConfig() 中有一个硬编码的表名.但是如何设置它才能知道我正在从多个表中请求数据.另外,如果我想设置关系,我是否仍然像在专辑教程中那样为数据库表创建类,或者它会有所不同.你能帮忙,或者告诉我正确的道路吗?如果您知道任何解释处理多个表的教程,那就太好了.

I'm new to Zend Framework 2. I successfully completed the Album tutorial for ZF2. Now I'd like to display only a certain data from multiple tables in the database. I have a simple database setup with tables, for example, person, books, status..etc. It's really not important what the database's supposed to do. What I would like to know is if there's a tutorial that would show me step-by-step guidance to display data from table joins. I have seen snippets of codes showing how to do joins, but I haven't found any tutorials on setting up the classes, and how to configure Module.php. In other words, the Module in Album has one table name hardcoded in getServiceConfig(). But how do I set it up so it knows I'm requesting data from multiple tables. Also, if I want to setup the relationship, do I still create class for database tables like in Album tutorial, or is it going to be something different. Can you please help, or show me the right path? If you know of any tutorial that explains handling multiple tables, that would be great.

推荐答案

ablums 教程使用了 Zend\Db\TableGateway,它不支持加入多个表.

The ablums tutorial uses Zend\Db\TableGateway which does not support joining to multiple tables.

您需要直接或通过映射器类使用 Zend\Db,例如 ZfcBase 模块中的 AbstractDbMapper.

You need to use Zend\Db directly or via a mapper class, such as AbstractDbMapper within the ZfcBase module.

基本用法如下:

<?php

// Given that $dbAdapter is an instance of Zend\Db\Adapter\Adapter

use Zend\Db\Sql\Select();
use Zend\Db\ResultSet\ResultSet();

$select = new Select();
$select->from('album')
   ->columns(array('album.*', 'a_name' => 'artist.name'))
   ->join('artist', 'album.artist_id' = 'artist.id');

$statement = $dbAdapter->createStatement();
$select->prepareStatement($dbAdapter, $statement);
$driverResult = $statment->execute();

$resultset = new ResultSet();
$resultset->initialize($driverResult); // can use setDataSource() for older ZF2 versions.

foreach ($resultset as $row) {
        // $row is an ArrayObject
}

join() 方法用于执行albumartist 表之间的连接.我们还使用 columns() 来选择返回哪些列.在本例中,我为艺术家表中的 name 列创建了一个名为 a_name 的别名.

The join() method is used to perform the join between the album and artist table. We also use columns() to select which columns are returned. In this case, I create an alias called a_name for the name column within the artist table.

一旦设置了 Select 对象,剩下的就是标准的 Db 代码,它将为您返回一个 ResultSet 对象,其中包含数据.

Once the Select object is set up, then the rest is the standard Db code that will return a ResultSet object for you containing the data.

这篇关于Zend Framework 2 的多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆