1

需要帮助了解为什么不使用 ServiceManager 就无法查询我的数据库,或者可能是我做错了什么。可能不推荐我的方法,但您的回答将帮助我更好地理解框架。

我的模型如下:

namespace Album\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\Adapter;

class AlbumTable
{
    public function getAll()
    {
        $configArray = ['driver' => 'Pdo_Mysql', 'database' => 'zf2tutorial', 'username' => 'root'];
        $adapter = new Adapter($configArray);
        $tableGateway = new TableGateway('Album', $adapter);
        $resultSet = $tableGateway->select();
        return $resultSet;
    }
}

我的控制器:

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\AlbumTable;

class AlbumController extends AbstractActionController
{
    public function indexAction()
    {
        $rowset = new AlbumTable();
        $rowset->getAll();
        return new Viewmodel(array(
            'rows' => $rowset
        ));
    }
}

对应的查看文件:

var_dump($this->rows) 
// outputs: object(Album\Model\AlbumTable)[250].

谢谢。

4

1 回答 1

2

您传入的是 AlbumTable 对象,而不是返回 ResultSet 的 getAll() 的结果。

$rows = $rowset->getAll();
return new Viewmodel(array(
    'rows' => $rows
));
于 2013-03-07T09:22:48.780 回答