zend-dbzend-viewzend-framework3

how can partialLoop viewhelper be used in zend3 with tablegateway


I still have trouble to get used to Zend3 framework. Before I have used Zend 1, it might be still an understanding problem due to tablegatewayadapter. I just want to show 2 entities together, which would be projects 1-n units.So I have to pass the projectkey somehow to my unitarray to get only the related units. In Zend1 I realised this (in another example) with a partialloop helper which shows the n entity, here are the relations "veranstaltung - docs", same idea, other tables.

This is a snippet, that shows what I former did in zend1:

    $veran=new Application_Model_DbTable_Ribaveranstaltungen();
$documents = new Application_Model_DbTable_Ribadocs();
$select=$veran->select()
->from('riba_veranstaltung')
->order('sort DESC');

$veranstaltung=$veran->fetchAll($select);
foreach($veranstaltung as $v) : 
    $dokument=$documents->getDocumentveranstaltung1($v->id);?>
    <tr>
        <td> <a href="<?php echo $this->url(array('controller'=>'Ribaveranstaltungen',
            'action'=>'index'));?>"><img src="./images/icons/haus.jpg" width="20" height="20" /></a></td>
        <td class="row_0"><?php echo $v->veranstaltung;?></td>
        <td class="row_1"><?php echo $this->partialLoop('/helpers/_docs-row.phtml', $dokument);?></td>
    </tr>
<?php   

    $j=$j+1;
endforeach;?>

How to migrate this to zend3. I had several ideas, for example 1.idea: I tried within with my controller:

public function indexAction()
{
    //'imports' => $this->table->fetchAll(),
    return new ViewModel([
            'projects' => $this->projectTable->fetchAll(),
            'units' => $this->unitTable->fetchAll(),
    ]);
}

Here is my trouble, how to use my method fetchALLP(project), because I want to pass the actual projectkey to the units to get only the related records.

2.idea: I tried to do just the same, as I did in Zend1. I wanted to initite my object in my view.

$gateway = new Unit();
$units = new UnitTable($gateway);
foreach ($projects as $project) : 
    $unit=$units->fetchAllP($project->ProjectID);?>

This, I just expected it, doesn't work, because I really didn't understand this tableadapter concept. Can somebody please explain it to me close to my example?

The next interesting topic would be, is there still a partialloop helper or do I have to use a new concept also in this case?

EDIT: to show the newest version

I changed my Controller/index action as suggested, it looks now:

        $result = $this->unitTable->fetchAll();
    $units = $result->toArray();
            return new ViewModel([
            'projects' => $this->projectTable->fetchAll(),
            'units' => $units,
    ]);

my index phtml (snippet):

foreach ($projects as $project) : 
?>
<tr>
<td><?= $this->escapeHtml($project->Projectname) ?></td>
 <td><?= $this->escapeHtml($project->PShortcut) ?></td>
 <td><?= $this->escapeHtml($project->PCI_Number) ?></td>
 <td><?= $this->escapeHtml($project->PDescription) ?></td>
 <td><?= $this->escapeHtml($project->PComponent_Class) ?></td>

<?php   $this->partialLoop('import/unit/index.phtml', $units);   

 endforeach; ?>

My partial (snippet of course there is and end of for)

<?php 

foreach ($units as $unit) : 
var_dump(get_object_vars($units));

?>
 <tr> 
<td><?= $this->escapeHtml($this->Unitname) ?></td>

EDIT: New experiences

I implemented now like this, the other suggestions didn't show the partial at all:

 $this->partialLoop()->setObjectKey('ProjectID');
echo $this->partialLoop('import/unit/unitpartial.phtml', $units);

I get this result: Screenshot output

It shows the first project and afterwards all units. It repeats the units with the number of projects, but doesn't show any other project.

I'm a bit further in any case. I need additional inforation to how can I link the partial to the actual project, I need in the view for example:

project1 Unit x Unit y project2 Unit abc ...


Solution

  • In the partialLoop view helper you can pass data or variable as an associative array or an object that implements toArray() method or an object that has an implementation of get_object_vars() for getting its public properties.

    public function testAction()
    {
        $result = $this->unitTable->fetchAll();
        $units = $result->toArray();
    
        return new ViewModel([
            'projects' => $this->projectTable->fetchAll(),
            'units' => $units,
        ]);
    }
    

    First, we would see an example passing an array of data.

    We would use $units variable, in this case, because it is an array of data as we converted it to an array.

    echo $this->partialLoop('/path/to/partial.phtml', $units);
    

    Now you can output table column name as a variable in your partial template, for example, in /path/to/partial.phtml.

    echo $column_name;
    // or 
    echo $this->column_name;
    

    Now, we would see an example passing an object as data.

    Guessing $projects variable has not converted into an array, it is an object. You may want to have it passed as an object to the partial script. You can do this by setting the objectKey property like the following

    $this->partialLoop()->setObjectKey('project');
    echo $this->partialLoop('/path/to/partial.phtml', $projects);
    

    Now in your partial /path/to/partial.phtml template, output object properties like the following

    echo $this->project->property_name;