访问视图以加入数据
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']
,但没有任何效果。
编辑:
我的控制器:
public function indexAction()
{
$album = $this->getAlbumTable()->getAllAlbum();
return new ViewModel(array(
'listAlbum' => $album
));
}
和视图:
<?
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) }
谢谢预付
PokeRwOw
你可以使用
`return new ViewModel(array(
'albums' => $resultSet,
));`
在你的函数中 'getAllAlbum' 而不是 return $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))`
只select你想要的字段
然后你可以使用$album = $album->toArray();
将对象转换成数组
并在视图中使用:
foreach($album as $alb){ echo $alb['singer_name']; }
我终于找到了我想要的。如果你想加入 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;
}
来源:
我目前正在处理 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']
,但没有任何效果。
编辑:
我的控制器:
public function indexAction()
{
$album = $this->getAlbumTable()->getAllAlbum();
return new ViewModel(array(
'listAlbum' => $album
));
}
和视图:
<?
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) }
谢谢预付
PokeRwOw
你可以使用
`return new ViewModel(array(
'albums' => $resultSet,
));`
在你的函数中 'getAllAlbum' 而不是 return $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))`
只select你想要的字段
然后你可以使用$album = $album->toArray();
将对象转换成数组
并在视图中使用:
foreach($album as $alb){ echo $alb['singer_name']; }
我终于找到了我想要的。如果你想加入 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;
}
来源: