在 Zend Framework 应用程序中,它与ZF2 入门教程中的专辑应用程序非常相似,我使用 ResultSet 对象将数据从模型通过控制器传输到视图(有关详细信息,请参见下面的代码)。
这很好用,但我不知道ResultSet
对象保存数据的位置。我可以循环它,例如,foreach
逐行获取数据(或逐个模型对象更好的模型对象)。但是当我在我的 IDE 中调试它或简单地使用它时var_dump(...)
,它似乎是空的。
对象如何/在哪里Zend\Db\ResultSet\ResultSet
保存从数据库中检索的数据?
代码的相关部分:
模块设置:
class Module implements ConfigProviderInterface, ServiceProviderInterface, AutoloaderProviderInterface {
...
public function getServiceConfig() {
try {
return array (
'factories' =>array(
...
'SportTable' => function ($serviceManager) {
$tableGateway = $serviceManager->get('SportTableGateway');
$table = new SportTable($tableGateway);
return $table;
},
'SportTableGateway' => function ($serviceManager) {
$dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Sport());
return new TableGateway('sports', $dbAdapter, null, $resultSetPrototype);
},
...
)
);
}
...
}
型号(表):
class CourseTable {
...
public function findAllByCityNameAndSportTitle($cityName, $sportTitle) {
$select = new Select();
$where = new Where();
...
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
...
}
模型(映射器):
class Course implements ArraySerializableInterface {
public $id;
...
public function exchangeArray(array $data) {
$this->id = (isset($data['id'])) ? $data['id'] : null;
...
}
...
}
控制器:
class CatalogController extends AbstractActionController {
...
public function listCoursesAction() {
$cityName = $this->params()->fromRoute('city', null);
$sportTitle = $this->params()->fromRoute('sport', null);
return new ViewModel(array(
'city' => $cityName,
'sport' => $sportTitle,
'courses' => $this->getCourseTable()->findAllByCityNameAndSportTitle($cityName, $sportTitle)
));
}
...
}
看法:
<?php foreach ($courses as $course) : ?>
<div>
<div>id: <?php echo $this->escapeHtml($course->id); ?></div>
...
</div>
<?php endforeach; ?>