如何在zf2中将数据从文档显示到index.phtml


how to show data from document to index.phtml in zf2?

我做了索引操作,我想在其中获得日历的数据,并使用index.phtml显示这些数据,但总是没有显示日历,我如何显示日历数据?这是我的索引:

 public function indexAction()
    {
        $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
        $qb = $dm->createQueryBuilder('Calendar'Document'Calendar')->select('title', 'description');
        $query = $qb->getQuery();
        $calendars = $query->execute();

            return array('calendars' => $calendars);
    }

这是我的index.phtml:

 <?php
$calendars = $this->calendars;
$title = 'Calendars by  '.$this->escapeHtml($calendars[0]->email);
$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>owner</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>
        <?php echo $this->gravatar($this->escapeHtml($calendar->email));?>
        <?php echo $this->escapeHtml($calendar->email);?>
    </td>
    <td>
        <a href="<?php echo $this->url('calendar',
            array('action'=>'settings', 'id' => $calendar->calendar_id));?>">Settings</a>
        <a href="<?php echo $this->url('calendar',
            array('action'=>'delete', 'id' => $calendar->calendar_id));?>">delete</a>
    </td>
</tr>
<?php endforeach; ?>
</table>

这是我的回应:

Doctrine''ODM''MongoDB '' Cursor Object([baseCursor:Doctrine''ODM''MongoDB ''Cursor:private]=>Doctrine''MongoDB ''Cursor对象([connection:protected]=>Doctrine''MongoDB ''connection Object([mongo:protected]=>MongoClient对象([已连接]=>1[状态]=>[服务器:受保护]=>[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

如何在索引页上显示数据?

您需要一个视图模型来呈现数据。代替

return array('calendars' => $calendars);

你想要这个作为一个视图:

$viewModel  =   new ViewModel
                    (
                        array
                        (
                            'calendars' =>  $calendars,
                        )
                    );
return $viewModel;

或者Json:

$jsonModel  =   new JsonModel
            (
            array
            (
                'calendars' =>  $calendars,
                            )
            );
return $jsonModel;

确保添加控制器的使用语句:

use Zend'View'Model'ViewModel;
use Zend'View'Model'JsonModel;

如果你想指定一个特定的视图,你可以使用:

$viewModel->setTemplate('path/to/specific/view.phtml');

$viewModel->setTemplate('mapping/for/specifc/view');

使用模块配置中指定的映射