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].
ありがとう。