Zend framework2 Zend 2 module.config带有模型/控制器未检索记录的约束

Zend framework2 Zend 2 module.config带有模型/控制器未检索记录的约束,zend-framework2,Zend Framework2,我正在学习Zend 2,有一个我似乎无法回避的问题。我确信这是件简单的事情,但我不能精确地指出它。我试图做的是按state_id列出记录,并从列表中查看记录。仅显示其中一个视图。当我打开/Blog/view/1时,我收到一条消息:找不到第0行。这将与当前链接一起显示,如果我关闭/1。因此,它甚至没有查看记录数字。我怀疑它可能在路由中,但不确定。谢谢 这就是我所拥有的: Module.config 'router' => array( 'routes' => array(

我正在学习Zend 2,有一个我似乎无法回避的问题。我确信这是件简单的事情,但我不能精确地指出它。我试图做的是按state_id列出记录,并从列表中查看记录。仅显示其中一个视图。当我打开/Blog/view/1时,我收到一条消息:找不到第0行。这将与当前链接一起显示,如果我关闭/1。因此,它甚至没有查看记录数字。我怀疑它可能在路由中,但不确定。谢谢 这就是我所拥有的:

Module.config
'router' => array(
  'routes' => array(      
      'blog' => array(        
        'type'    => 'Literal',
        'options' => array(
            'route'    => '/Blog',
            'defaults' => array (
              'module'     => 'Blog',
              'controller' => 'BlogController',
              'action'     => 'index',
            ), // defaults end 
        ), // options end           
        'may_terminate' => true,
            'child_routes' => array(
                'list' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/list[/:state_id]',
                        'defaults' => array (
                            'action'     => 'list',
                        ), //defaults end
                        'constraints' => array(
                            'state_id'     => '[0-9]+',
                        ) // constraints end
                    ), // options end
                ), // view end
                'view' => array(
                    'type'    => 'segment',
                    'options' => array(
                        'route'    => '/view[/:post_id]',
                        'defaults' => array(
                            'action'     => 'view',
                        ), // defaults end
                        'constraints' => array(
                        'post_id'     => '[0-9]+',
                        ) // constraints end
                    ), // options end
                ),  // post end 
            ) // child route end            
        ), // blog end
    ) // routes end       
) // router end

); // return array end
模型:PostTable.php

 public function getPosts($post_id)
 {
   $post_id  = (int) $post_id;
   $rowset = $this->tableGateway->select(array('post_id' => $post_id));
   $row = $rowset->current();
   if (!$row) {
       throw new \Exception("Could not find row $post_id");
   }
   return $row;
 }
视图:

<h1><?php echo $this->escapeHtml($title); ?></h1>
<table class="table">
<tr>
   <th>Title</th>
   <th>View</th>
   <th>Comments</th>
   <th>Post Date</th>
</tr>
<?php foreach ($list as $posts) : ?>
<tr>
  <td><?php echo $this->escapeHtml($posts->post_title);?></td>
  <td><?php echo $this->escapeHtml($posts->num_views);?></td>
  <td><?php echo $this->escapeHtml($posts->num_comments);?></td>
  <td><?php echo $this->escapeHtml($posts->post_date);?></td>
</tr>
<?php endforeach; ?>
下面是getPostTable操作:

 public function getPostsTable()
 {   
     if (!$this->postsTable) {
        $sm = $this->getServiceLocator();
        $this->postsTable = $sm->get('Blog\Model\PostsTable');
     }
     return $this->postsTable;
 }

您需要在控制器中的
getPosts()
方法中提供post ID。下面是根据您的
PostTable
getPosts()
方法编写的

public function viewAction()
{

    // Get the post ID from the route, /Blog/view/1
    $post_id = $this->params()->fromRoute('post_id');

    $list = $this->getPostsTable()->getPosts($post_id);

    $view = new ViewModel(array(
        'list' => $list,
    ));

    return $view;
}

请给我看一下
viewAction()
好吗?@J.Sajeeb添加了查看操作。这是取自zend 2的专辑教程。它显示了您在模型中获取相册的内容,但没有显示如何在控制器中检索它或视图的实际外观。谢谢你的工作!如何让它获取具有相同Id的多行?例如,如果我想提取所有state_id的记录,这将不是行或记录。以上内容适用于查看页面的单个记录。现在我需要从那个州提取所有记录。谢谢您的帮助。您需要创建另一个方法或修改
PostTable
getPosts()
方法。此方法通过这行代码
$row=$rowset->current()返回当前帖子/项目。例如,通过注释掉这行代码和总计
if(!$row)
语句并返回
return$rowset,可以提取
state\u id
的所有记录
而不是
返回$row!我还在另一个博客下发布了另一个问题。你可以在这里找到它:
public function viewAction()
{

    // Get the post ID from the route, /Blog/view/1
    $post_id = $this->params()->fromRoute('post_id');

    $list = $this->getPostsTable()->getPosts($post_id);

    $view = new ViewModel(array(
        'list' => $list,
    ));

    return $view;
}