multiple tables for Zend Framework 2

淺唱寂寞╮ 提交于 2019-12-02 17:20:52

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

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

The basic usage looks like this:

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

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.

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.

Andrew

Just to expand on Robs excellent answer, its simple to take it a step further and populate multiple objects to form the relationship you require.

<?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.*', 'artist.*'))
   ->join('artist', 'album.artist_id' = 'artist.artist_id');

$statement = $dbAdapter->createStatement();
$select->prepareStatement($dbAdapter, $statement);
$driverResult = $statement->execute(); // execute statement to get result

$resultset = new ResultSet();
$resultset->setDataSource($driverResult);

$albumHydrator = new AlbumHydrator;
$artistHydrator = new ArtistHydrator;

foreach($resultset as $row) { // $row is an ArrayObject
    $album = $albumHydrator->hydrate($row);
    $artist = $artistHydrator->hydrate($row);
    $album->setArtist($artist);
}

You should also look at hydrating result sets to build your objects for you directly from the ResultSet:

http://framework.zend.com/manual/2.0/en/modules/zend.db.result-set.html

use Zend\Db\Sql\Select;
$select = new Select();
// or, to produce a $select bound to a specific table
// $select = new Select('foo');

$select->join(
     'foo' // table name,
     'id = bar.id', // expression to join on (will be quoted by platform object before insertion),
     array('bar', 'baz'), // (optional) list of columns, same requiremetns as columns() above
     $select::JOIN_OUTER // (optional), one of inner, outer, left, right also represtned by constants in the API
);

$select->from(array('f' => 'foo'))  // base table
    ->join(array('b' => 'bar'),     // join table with alias
    'f.foo_id = b.foo_id');         // join expression
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!