Cakephp3- foreach loop iteration failed

I’m fetching a list of users as an array and paginate the data and show it in the view as a table.

To iterate the array, I use foreach loop. But my foreach loop iteration is not working.

Here’s a sample array when I did print_r() I have id, email and full_name fields which I wanna display in view.

Cake\ORM\ResultSet Object
(
    [items] => Array
        (
            [0] => App\Model\Entity\Role Object
                (
                    [_matchingData] => Array
                        (
                            [Users] => App\Model\Entity\User Object
                                (
                                    [id] => 1
                                    [email] => johndoe@doe.com
                                    [full_name] => John Doe
                                    [[new]] => 
                                    [[accessible]] => Array
                                        (   [*] => 1 )
                                    [[dirty]] => Array
                                        ( )
                                    [[original]] => Array
                                        ( )
                                    [[virtual]] => Array
                                        ( )
                                    [[errors]] => Array
                                        ( )
                                    [[invalid]] => Array
                                        (  )
                                    [[repository]] => Users
                                )
                        )

                    [[new]] => 
                    [[accessible]] => Array
                        ( [*] => 1 )
                    [[dirty]] => Array
                        ( )
                    [[original]] => Array
                        (  )
                    [[virtual]] => Array
                        ( )
                    [[errors]] => Array
                        (  )
                    [[invalid]] => Array
                        (  )
                    [[repository]] => Roles
                )
        )
)

This is the part of view where I iterate the array. Result is stored in $userList. There’s always more than one user in this array so I use a foreach loop.

<?php foreach ($userList as $user): ?>
<tr>
    <td><?= h($user->id) ?></td>
    <td><?= h($user->email) ?></td>
    <td><?= h($user->full_name) ?></td>
</tr>
<?php endforeach; ?>

In my view, the rows get generated but I don’t see the array values. If there are five users in the array, 5 rows are generated but values aren’t displayed. What’s wrong with the iteration I used? Can anyone point out my mistake? Thanks!

You’re iterating the array of Role Objects, not the User object.

<?php foreach ($userList as $user): ?>
<tr>
    <td><?= h($user->_matchingData['Users']->id) ?></td>
    <td><?= h($user->_matchingData['Users']->email) ?></td>
    <td><?= h($user->_matchingData['Users']->full_name) ?></td>
</tr>
<?php endforeach; ?>
1 Like