0

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 ...

pia-sophie
  • 505
  • 4
  • 21
  • Please be specific with your exact issue. It seems you are coming across tablegateway and viewhelper issues. Would you let us know which one you just cant make work? – unclexo Jun 13 '17 at 08:35
  • you are right might be both. any helpful tipps? – pia-sophie Jun 13 '17 at 09:14
  • Did you follow the [doc](https://docs.zendframework.com/tutorials/getting-started/database-and-models/) for how to make the tablegateway available using servicemanager? – unclexo Jun 13 '17 at 10:38
  • Yes, I think so, because ist works alone. I have only trouble to show different connected tables. All forms/views that show a single table work properly. Is there somewhere a documentation/tutorial that shows, how to vonnect different tables? – pia-sophie Jun 13 '17 at 10:44
  • Well, that's good! For the partial loop view helper you can check out this [doc](https://docs.zendframework.com/zend-view/helpers/partial/). – unclexo Jun 13 '17 at 10:53
  • as you can see in the snippets, I used it before but I'm a bit confused how to pass the data there in zend3. I cannot do it the same, I tried of course, because of the tablegateway construction which using I didn't get right I think. I can use ist with a singee table, but not with two tables. Can give me a tip where to search or just how to do it properly? – pia-sophie Jun 13 '17 at 11:04

1 Answers1

1

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;
unclexo
  • 3,691
  • 2
  • 18
  • 26
  • I tried your proposal and get an error "This result is a forward only result set, calling rewind() after moving forward is not supported". I passed the unit to the partial because that's the recordset I want to show there, how do I pass the ID of the actual project to the partial? – pia-sophie Jun 13 '17 at 14:51
  • 1
    Just edited code! It is tested and working for the partial templates. – unclexo Jun 13 '17 at 17:33
  • Edited once again! Now you can pass both an object or array of data to the partial script. – unclexo Jun 14 '17 at 09:35
  • I understand the concept, but I get an error: "Rows as part of this DataSource, with type object cannot be cast to an array" – pia-sophie Jun 14 '17 at 09:47
  • I added a function getArrayCopy(), the error is gone, but the partial won't show. I will edit my Post and add my partial. – pia-sophie Jun 14 '17 at 09:58
  • Just use this `= $this->escapeHtml($this->Unitname) ?>` in you partial script without any loop. – unclexo Jun 14 '17 at 10:20
  • it is just the same – pia-sophie Jun 14 '17 at 10:27
  • what are you getting now? – unclexo Jun 14 '17 at 10:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146641/discussion-between-j-sajeeb-and-pia-sophie). – unclexo Jun 14 '17 at 10:44