Question

Need help understanding why I can't query my database without using the ServiceManager or maybe it's something else I'm doing wrong. My approach is probably not recommended but your answers would help me better understand the framework.

My Model is as follows:

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;
    }
}

My Controller:

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
        ));
    }
}

Corresponding View File:

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

Thanks.

Was it helpful?

Solution

You are passing in the AlbumTable object, not the result of getAll() which returns your ResultSet.

$rows = $rowset->getAll();
return new Viewmodel(array(
    'rows' => $rows
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top