Search code examples
zend-frameworkzend-view

Using Zend partialLoop Viewhelper with an object


I wanted to learn a bit more about view helpers, epecially partialLoop helpers. I tried with the following code snippets, but it does't work.

I built a partialLoop helper:

<tr>
<td class='links'>
    <a href='/user/update/id/<?php echo $this->id;?>'>Update</a>
    <a href='/user/delete/id/<?php echo $this->id;?>'>Delete</a>
</td>
<td><?php echo $this->lastname ?></td>
<td><?php echo $this->firstname ?></td>
<td><?php echo $this->username ?></td>
<td><?php echo $this->role ?></td>

I want to implement it in my view:

    <?php
if($this->users != null)
{?>
    <table class='spreadsheet' cellpadding='0' cellspacing='0'>
    <tr>
        <th>links</th>
        <th>Nachname</th>
        <th>Vorname</th>
        <th>Username</th>
        <th>Role</th>
    </tr>
    <?php echo $this->partialLoop('partials/_user-row.phtml', $this->users); ?>
    </table>
    <?php
    }
    else
    { ?>
    <p> Keine Benutzer vorhanden </p>
    <?php 
    } ?>
    <p><a href='/user/create'>Neu</a></p>
`$this->users` is an object of `Zend_Db_Table_Abstract`

I get this error: PartialLoop helper requires iterable data

What does this mean in this case? Of course I get probably more than 1 record with userdata out of my database. How can I solve the problem? Which basic error I made? I just read the ZF tutorial but I didn´t get the answer,


Solution

  • Hope the below example code will helps you:

    Example code for "partials/_user-row.phtml":

    <tr>
        <td><?php echo $this->user->firstname ?></td>
    </tr>
    

    Example code for your other .phtml file:

    <table>
        <thead>
            <tr>
                <th>
                    <?php echo "First Name" ?>
                </th>
            </tr>
        </thead>
        <tbody>
            <?php  $this->partialLoop()->setObjectKey('user');
                  echo $this->partialLoop('partials/_user-row.phtml', $this->users);
            ?>
        </tbody>
    </table>