我做了一个索引操作,其中我使用doctrine查询从日历表中选择数据,我想使用index.phtml显示数据,但我的数据不显示只显示空白页,我如何显示控制器中的数据? 这是我的代码:
public function indexAction()
{
$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$qb = $dm->createQueryBuilder('Calendar\Document\Calendar');
$query = $qb->getQuery();
$calendars = $query->execute();
return array('calendars' => $calenders)
}
这是我的index.phtml代码:
<?php
$calendars = $this->calendars;
$title = 'Calendars by';
$this->headTitle($title);
?>
<h3><?php echo $this->escapeHtml($title); ?></h3>
<ul>
<li><a href="<?php echo $this->url('calendar', array('action'=>'create'));?>">Create New Calendar</a></li>
</ul>
<h4>Calendars created by you</h4>
<?php if (is_null($calendars)): ?>
<p>No calendars</p>
<?php else: ?>
<table class="table">
<tr>
<th>calendar name</th>
<th>description</th>
<th>actions</th>
</tr>
<?php foreach ($calendars as $calendar) : ?>
<tr>
<td>
<a href="<?php echo $this->url('calendar',array('action'=>'show', 'id' => $calendar->calendar_id));?>">
<?php echo $this->escapeHtml($calendar->title);?>
</a>
</td>
<td><?php echo $this->escapeHtml($calendar->description);?></td>
<td>
<a href="<?php echo $this->url('calendar',
array('action'=>'settings', 'id' => $calendar->_id));?>">Settings</a>
<a href="<?php echo $this->url('calendar',
array('action'=>'delete', 'id' => $calendar->_id));?>">delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
以下是我的回复:
Doctrine\ODM\MongoDB\Cursor Object
(
[baseCursor:Doctrine\ODM\MongoDB\Cursor:private] => Doctrine\MongoDB\Cursor Object
(
[connection:protected] => Doctrine\MongoDB\Connection Object
(
[mongo:protected] => MongoClient Object
(
[connected] => 1
[status] =>
[server:protected] =>
[persistent:protected] =>
)
[server:protected] => mongodb://127.0.0.1:27017/events
[options:protected] => Array
(
)
[config:protected] => Doctrine\ODM\MongoDB\Configuration Object
(
[attributes:protected] => Array
(
[mongoCmd] => $
[retryConnect] => 0
[retryQuery] => 0
[autoGenerateProxyClasses] => 1
[proxyDir] => data/DoctrineMongoODMModule/Proxy
[proxyNamespace] => DoctrineMongoODMModule\Proxy
[autoGenerateHydratorClasses] => 1
[hydratorDir] => data/DoctrineMongoODMModule/Hydrator
[hydratorNamespace] => DoctrineMongoODMModule\Hydrator
[defaultDB] => events
[metadataCacheImpl] => Doctrine\Common\Cache\ArrayCache Object
(
[data:Doctrine\Common\Cache\ArrayCache:private] => Array ...................................
我如何在index.phtml中显示查询结果?
答案 0 :(得分:1)
您需要将ViewModel返回到应用程序,并在关联数组中设置属性,否则将返回没有属性的默认ViewModel。
例如:
return new ViewModel(array(
'content' => 'foo bar!'
));
然后在.phtml文件中:
<p><?php print $this->content; ?></p>