在视图中访问连接数据


Access in view to join data

我目前正在做一个Zend 2项目,我在访问视图中的连接数据时遇到了一些问题。

首先,我创建了在AlbumTable中获取数据的函数:

public function getAllAlbum()
{
    $select = new Select();
    $select->from('album')
            ->join('singer', 'singer.id = album.id')
            ->order('album.id');
    $resultSet = $this->tableGateway->selectwith($select);
    $resultSet->buffer();
    return $resultSet;
}

这个函数给我所有我想要的数据。但现在在视图中,我想访问这些数据。我尝试了很多东西,如:->id_album->singer.name, ->id_album->singer['name'], ->singer.name, ->singer['name'],但没有工作。

编辑:

my controller:

public function indexAction()
{
    $album = $this->getAlbumTable()->getAllAlbum();
    return new ViewModel(array(
        'listAlbum' => $album
    ));
}

和view:

<?
foreach ($this->listAlbum as $param)
{?>
    <tr class="tab_line">
        <td style="width:30%;">
            <a><?php echo $this->escapeHtml($param->name);?></a>
        </td>
        <td style="width:30%;">
            <a><?php echo $this->escapeHtml($param->id_album->nom);?></a>
        </td>
    </tr>
<?}?>

var_dump美元专辑:

object(Zend'Db'ResultSet'ResultSet)#420 (8) { 
["allowedReturnTypes":protected]=> array(2) { 
  [0]=> string(11) "arrayobject" 
  [1]=> string(5) "array" } 
    ["arrayObjectPrototype":protected]=> object(Check'Model'Album)#405 (4) {
    ["inputFilter":protected]=> NULL ["id"]=> NULL ["name"]=> NULL 
    ["id_singer"]=> NULL } ["returnType":protected]=> string(11) "arrayobject" 
    ["buffer":protected]=> array(0) { } ["count":protected]=> int(1) 
    ["dataSource":protected]=> object(Zend'Db'Adapter'Driver'Pdo'Result)#419 (8) { 
    ["statementMode":protected]=> string(7) "forward" 
    ["resource":protected]=> object(PDOStatement)#411 (1) { 
    ["queryString"]=> string(127) "SELECT `album`.*, `singer`.* FROM `album` INNER JOIN `singer` ON `singer`.`id` = `album`.`id` ORDER BY `album`.`id` ASC" } 
    ["options":protected]=> NULL ["currentComplete":protected]=> bool(true)
    ["currentData":protected]=> array(4) { ["id"]=> string(1) "2" 
    ["name"]=> string(9) "Test" ["id_singer"]=> string(1) "2"
    ["name"]=> string(5) "Test2" } ["position":protected]=> int(0) 
    ["generatedValue":protected]=> string(1) "0" ["rowCount":protected]=> int(1) } 
    ["fieldCount":protected]=> int(5) ["position":protected]=> int(0) }

Thanks per advance

PokeRwOw

我终于找到我想要的了。如果您想与Zend 2.0和对象建立连接,您需要做这样的事情(在我的情况下):

public function getAllAlbum()
{
    $select = new Select();
    $select->from('album')
        ->join('singer', 'singer.id = album.id')
        ->order('album.id');
    $statement = $this->tableGateway->getSql()->prepareStatementForSqlObject($select);
    $resultSet = $statement->execute();
    $resultSet->buffer();
    return $resultSet;
}

来源:https://stackoverflow.com/a/25983788/3752471

可以使用

   `return new ViewModel(array(
         'albums' => $resultSet,
     ));`

在函数'getAllAlbum'中,而不是只返回$resultSet

然后像这样用foreach循环相册:

foreach($albums as $album){ echo $album['title']; }

编辑1

你可以修改:

`->join('singer', 'singer.id = album.id')'`

:

`->join('singer', 'singer.id = album.id', array(singer_name, whateveryouwant))`

只选择你想要的字段

然后可以使用$album = $album->toArray();将对象转换为数组

在视图中使用:foreach($album as $alb){ echo $alb['singer_name']; }