【问题标题】:Construct() must be an instanceConstruct() 必须是一个实例
【发布时间】:2013-12-08 23:55:50
【问题描述】:

我正在关注ZF2 Manual,但我遇到了这个错误:

"可捕获的致命错误:传递给 Album\Model\AlbumTable::__construct() 的参数 1 必须是 Zend\Db\TableGateway\TableGateway 的实例,给定 Zend\Db\Adapter\Adapter 的实例,在 /var 中调用/www/CommunicationApp/module/Album/Module.php 位于第 33 行,定义在 /var/www/CommunicationApp/module/Album/src/Album/Model/AlbumTable.php 的第 11 行"

我不知道我错过了什么,因为它与教程完全相同。

<?php

namespace Album\Model;

use Zend\Db\TableGateway\TableGateway;

class AlbumTable
{
 protected $tableGateway;

 public function __construct(TableGateway $tableGateway)
 {
     $this->tableGateway = $tableGateway;
 }

 public function fetchAll()
 {
     $resultSet = $this->tableGateway->select();
     return $resultSet;
 }

 public function getAlbum($id)
 {
     $id  = (int) $id;
     $rowset = $this->tableGateway->select(array('id' => $id));
     $row = $rowset->current();
     if (!$row) {
         throw new \Exception("Could not find row $id");
     }
     return $row;
 }

 public function saveAlbum(Album $album)
 {
     $data = array(
         'artist' => $album->artist,
         'title'  => $album->title,
     );

     $id = (int) $album->id;
     if ($id == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getAlbum($id)) {
             $this->tableGateway->update($data, array('id' => $id));
         } else {
             throw new \Exception('Album id does not exist');
         }
     }
 }

 public function deleteAlbum($id)
 {
     $this->tableGateway->delete(array('id' => (int) $id));
 }
 }

模块.php:

<?php
namespace Album;
use Album\Model\AlbumTable;


class Module
{
public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
        ),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
            ),
        ),
    );
}

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Album\Model\AlbumTable' =>  function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $table     = new AlbumTable($dbAdapter);
                return $table;
            },
        ),
    );
}
}

【问题讨论】:

    标签: php zend-framework web-applications zend-framework2


    【解决方案1】:

    您应该将 TableGetway 传递给 AlbumTable。更改 Module.php 并将 getServiceConfig 替换为:

     public function getServiceConfig()
     {
         return array(
             'factories' => array(
                 'Album\Model\AlbumTable' =>  function($sm) {
                     $tableGateway = $sm->get('AlbumTableGateway');
                     $table = new AlbumTable($tableGateway);
                     return $table;
                 },
                 'AlbumTableGateway' => function ($sm) {
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                     $resultSetPrototype = new ResultSet();
                     $resultSetPrototype->setArrayObjectPrototype(new Album());
                     return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                 },
             ),
         );
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多