警告:foreach()参数的类型必须是array|object,在Zend Framework中给定null



请帮我纠正这个错误

控制器:

class  AlbumController extends AbstractActionController
{
public $table;
public function __construct(AlbumTable $table)
{
$this->table = $table;
}
public function indexAction()
{
return new ViewModel([
'albums' => $this->table->fetchAll(),
]);
}
}

型号:

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

模块:

<?php
namespace Album;

// Add these import statements:

use LaminasDbAdapterAdapter;
use LaminasDbAdapterAdapterInterface;
use LaminasDbResultSetResultSet;
use LaminasDbTableGatewayTableGateway;
use LaminasModuleManagerFeatureConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig(): array
{
return include __DIR__ . '/../config/module.config.php';
}
// Add this method:
public function getControllerConfig()
{
return [
'factories' => [
ControllerAlbumController::class => function ($container) {
return new ControllerAlbumController(
$container->get(ModelAlbumTable::class)
);
},
],
];
}
public function getServiceConfig()
{
return [
'factories' => [
ModelAlbumTable::class => function($container) {
$tableGateway = $container->get(ModelAlbumTableGateway::class);
return new ModelAlbumTable($tableGateway);
},
ModelAlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new ModelAlbum());
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>

该错误发生在index.phtml文件中。请帮忙,我已经为这个错误挣扎了很长时间。如果你帮忙,我将不胜感激。我正在使用框架Laminas(zend(。我正试图从表中获取一个值

仅此而已,我正在等待你的建议

class AlbumTable {
private $tableGateway;
public function __construct(TableGatewayInterface $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
*return* $this->tableGateway->select();
}

最新更新