0

help me please this error

Controller :

    class  AlbumController extends AbstractActionController
{
    public $table;

    public function __construct(AlbumTable $table)
    {
        $this->table = $table;
    }

    public function indexAction()
    {
        return new ViewModel([
            'albums' => $this->table->fetchAll(),
        ]);
    }
    }

Model :

class AlbumTable {

    private $tableGateway;

    public function __construct(TableGatewayInterface $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }
    public function fetchAll()
    {
        $this->tableGateway->select();
    }

Module :

    <?php

namespace Album;



// Add these import statements:


use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
    public function getConfig(): array
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    // Add this method:
    public function getControllerConfig()
    {
        return [
            'factories' => [
                Controller\AlbumController::class => function ($container) {
                    return new Controller\AlbumController(
                        $container->get(Model\AlbumTable::class)
                    );
                },
            ],
        ];
    }

    public function getServiceConfig()
    {
        return [
            'factories' => [

                Model\AlbumTable::class => function($container) {
                    $tableGateway = $container->get(Model\AlbumTableGateway::class);
                    return new Model\AlbumTable($tableGateway);
                },
                Model\AlbumTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },

            ],
        ];
    }
}

index.phtml

    <?php
// module/Album/view/album/album/index.phtml:

$title = 'My albums';
$this->headTitle($title);
?>
<h1><?= $this->escapeHtml($title) ?></h1>
<p>
    <a href="<?= $this->url('album', ['action' => 'add']) ?>">Add new album</a>
</p>

<table class="table">
    <tr>
        <th>Title</th>
        <th>Artist</th>
        <th>&nbsp;</th>
    </tr>
    <?php
    foreach ($albums as $album) : ?>
        <tr>
            <td><?= $this->escapeHtml($album->title) ?></td>
            <td><?= $this->escapeHtml($album->artist) ?></td>
            <td>
                <a href="<?= $this->url('album', ['action' => 'edit', 'id' => $album->id]) ?>">Edit</a>
                <a href="<?= $this->url('album', ['action' => 'delete', 'id' => $album->id]) ?>">Delete</a>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

The error occurs in the index.phtml file. Please help, I've been struggling with this error for a long time. If you help I will be very grateful . i am use framework Laminas(zend) . I'm trying to get a value from a table

That's all, I'm waiting for your suggestions

3
  • It looks like the error is just saying is that $albums is null in the foreach loop: foreach ($albums as $album) ... which implies something's broken further up the line, e.g.: 'albums' => $this->table->fetchAll() is null in the controller's indexAction() Commented Feb 25, 2022 at 12:36
  • Do not know how to solve the problem? Commented Feb 25, 2022 at 12:40
  • I know how to make the error go away, add a simple sanity check before the loop, something like if(is_iterable($albums)) - but the real issue you need to find is higher up, why is $albums null in the first place. I'm guessing you want to query the database table and return the result set to the view (beyond what Magento 2 does, I'm not that familiar with the nuts and bolts of laminas I'm afraid); Commented Feb 25, 2022 at 12:47

1 Answer 1

2
class AlbumTable {

    private $tableGateway;

    public function __construct(TableGatewayInterface $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }
    public function fetchAll()
    {
        *return* $this->tableGateway->select();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Aaah looks like you found it - I missed the missing return as well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.