pia-sophie
pia-sophie

Reputation: 505

how to get several rows out of my model

This is again an understanding issue, any explanation is appreciated:

I before had the issue to show a foreignkey table in my view, that now works, it looks like follows:

screenshot to show result

That's quite what I wanted, but: I only get the first row, but there are of course several in the second collection (it is the row with filename).

It is because of my method in my model, which looks like follows:

    public function getImportU($unitid)
{
    $unitid = (int) $unitid;
    $rowset = $this->tableGateway->select(['UnitID' => $unitid]);
    $row = $rowset->current();
    if (! $row) {
        return null;
    }
    else{
        return $row;
    }
}

Of course I have a row object and it returns the current row. So I thought, ok I will try with a rowset, after that it looked like this:

public function getImportU($unitid)
{
    $unitid = (int) $unitid;
    $rowset = $this->tableGateway->select(['UnitID' => $unitid]);
    //$row = $rowset->current();
    if (! $rowset) {
        return null;
    }
    else{
        return $rowset;
    }
}

I got some errors which said:

Notice: Undefined property: Zend\Db\ResultSet\ResultSet::$Importdate in Blockquote

So how to get a recordcollection, all records there are in the table with the same unitid? And how to call them in the view. It is probably not a big deal, but I couldn't find anything usable in the documentation. EDIT1: Adding related code.

Here I pass the ViewModel within my Controller indexAction:

return new ViewModel([
                'projects' => $this->projectTable->fetchAll(),
                'dcls' => $this->table,
                    //'id' =>$this->authService,
                ]);

And here a snippet of my index.phtml:

 foreach ($projects as $project) :
//var_dump(get_object_vars($project));
?>
    <tr>
    <td><?= $project['Projectname']?></td>
    <td><?= $project['ProjectShortcut']?></td>
    <td><?= $project['ProjectCiNumber']?></td>
    <td><?= $project['Unitname']?></td>
    <td><?= $project['UnitShortcut']?></td>
    <td><?= $project['UnitCiNumber']?></td>
    <td><?= $project['UnitID']?></td>
     </tr>
     <?php   
     $dclsx=$dcls->getImportU($project['UnitID']);
    // var_dump($dclss);
     if ( empty ($dclsx)==false){ ?>
        <tr>
        <th></th>
        <th>filename</th>
        <th>importdate</th>
        <th>importuser</th>
        <th>importok</th>
        </tr>
    <?php 
        $dclss=array($dclsx);
    //    var_dump($dclss);
         foreach ( $dclss as $dcl) :
         ?>
         <tr>
            <td>   </td>
            <td><?= $dcl->filename?></td>
            <td><?= $dcl->Importdate?></td>
            <td><?= $dcl->Importuser?></td>
        </tr>
       ?php 
    endforeach;
       }
   endforeach; ?>

Upvotes: 0

Views: 44

Answers (1)

Jannes Botis
Jannes Botis

Reputation: 11242

I believe you are pretty close to the solution. "getImportU" returning a rowset is the correct solution as I see it:

public function getImportU($unitid)
{
    $unitid = (int) $unitid;
    $rowset = $this->tableGateway->select(['UnitID' => $unitid]);
    if (! $rowset) {
        return null;
    }
    else{
        return $rowset;
    }
}

Your view should be changed from:

<?php 
    $dclss=array($dclsx);
//    var_dump($dclss);
     foreach ( $dclss as $dcl) :

to:

<?php
     foreach ( $dclsx as $dcl) :

$dclsx is a resultset and can be iterated, it does not make sense to put it in an array.

Your error also suggests that "Importdate" cannot be found in the blockquote table. Please check if the column exists at the table(also check the name is case sensitive, is it "Importdate", "importdate" or "importDate"?)

Upvotes: 2

Related Questions