1

I'm currently working on a Zend 2 project and I have some problems to access the data of a join in the view.

So first, I created my function to get data in my 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;
}

This function give me all datas I wanted. But now in the view, I want to access to there data. I tried a lot of thing like : ->id_album->singer.name, ->id_album->singer['name'], ->singer.name , ->singer['name'] but nothing worked.

EDIT :

my controller :

public function indexAction()
{
    $album = $this->getAlbumTable()->getAllAlbum();

    return new ViewModel(array(
        'listAlbum' => $album
    ));
}

And the 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 $album:

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

PokeRwOw
  • 609
  • 1
  • 6
  • 29

2 Answers2

2

I finally found what I wanted. If you want to make a join with Zend 2.0 and object you need to do something like this (in my case) :

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

Source : https://stackoverflow.com/a/25983788/3752471

Community
  • 1
  • 1
PokeRwOw
  • 609
  • 1
  • 6
  • 29
0

You can use

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

in your function 'getAllAlbum' instead of just return $resultSet

Then you loop the albums with foreach like this :

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

Edit 1

You can change :

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

To :

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

To select only field you want

Then you can use $album = $album->toArray(); To convert object into array

And in the view use : foreach($album as $alb){ echo $alb['singer_name']; }

wilson
  • 334
  • 1
  • 15
  • Already doing this, I edit my post. I just want to access to the join data only, because in the view I have '$param->id_album->nom' but It's not working – PokeRwOw Aug 05 '15 at 09:39
  • Can you show us the value of `$album` ? what kind of data is ? – wilson Aug 05 '15 at 09:45
  • I added the var_dump of $abum. Strangely I can access to singer->name with $param->name but now I can't access to album->name – PokeRwOw Aug 05 '15 at 10:35
  • As you can see in the var_dump, the problem isn't coming from the request to db because I have all datas ({ ["id"]=> string(1) "2" ["name"]=> string(9) "Test" ["id_singer"]=> string(1) "2" ["name"]=> string(5) "Test2" }). But I don't know how to access to the variable name coming from the join... I tried your solution anyway and Its does not working – PokeRwOw Aug 05 '15 at 12:42
  • I come back on your solution, after $album->toArray(); in my controller, It's return me that : **array(1) { [0]=> array(4) { ["inputFilter"]=> NULL ["id"]=> string(1) "2" ["name"]=> string(9) "test" ["id_singer"]=> string(1) "2" } } **So I haven't got the singer's name but I added array(singer_name, whateveryouwant) in my model – PokeRwOw Aug 05 '15 at 13:55
  • No, I debugged the code. The request to db is right (I tested in mysql directly) So the problem is always the same, I can't access to singer_name in the view... – PokeRwOw Aug 06 '15 at 14:09