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

喜你入骨 提交于 2019-12-04 05:53:12

问题


i make index action in which i want to get data of calender and show this data using index.phtml but always no calendar shown,how i show calendar data? here is my indexaction:

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

    }

and here is my 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>

and here is my response:

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

how i show data on index page?


回答1:


You need a view model to render your data. Instead of

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

You want this for a View:

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

return $viewModel;

or this for Json:

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

return $jsonModel;

make sure to add the use statements for your controller:

use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;

If you want to specify a specific view you can use:

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

or

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

with the mapping specified in your module config



来源:https://stackoverflow.com/questions/23106290/how-to-show-data-from-document-to-index-phtml-in-zf2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!