为什么此错误1066出现在加入声明中

时间:2017-06-06 21:38:56

标签: mysql zend-framework2 tablegateway

我还有另一个问题。我试图加入2个表,以便我可以从两个表中提取数据。我是TableGateway的新手,遇到了同样情况的问题。我有一个页面,将按州拉博客,我也有一个州表。我想使用state_id加入这些并显示" state"从页面上的州表。以下是我所拥有的。不确定我是否使用了正确的代码!

模型 - 来自StateTable:

public function getState($state)
{
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->columns(array('state_id'));
$sqlSelect->join('states', 'states.state_id = state_id', array('state'), 'inner');

$statement =   $this->tableGateway->getSql()->prepareStatementForSqlObject($sqlSelect);
$resultSet = $statement->execute();
return $resultSet;

}

PostTable的模型:

   public function getbystate($state_id)
   {
    $state_id  = (int) $state_id;
    $rowset = $this->tableGateway->select(array('state_id' => $state_id));

    return $rowset;
   }

控制器操作:

 public function ListAction()
 {
  $state_id = $this->params()->fromRoute('state_id');
  $state = $this->params('state');
  $list = $this->getPostsTable()->getbystate($state_id);
  $states = $this->getStatesTable()->getState($state);
  $view = new ViewModel(array(
    'list' => $list,
    'states' => $states,
  ));

  return $view;  
 }

更新时间:美国东部时间6月41日06/06

我将连接放在Posts模块中,而不是在States Model中。 我已经摆脱了错误,但我仍然需要知道如何显示状态。以上所有内容都相同,只不过它位于posts模块中。

"查看"在下面 - 它错误 注意:未定义的属性:第6行的/var/www/html/module/Blog/view/blog/list.phtml中的Zend \ Db \ Adapter \ Driver \ Pdo \ Result :: $ state

查看:

 $title = 'My Blog';
 $this->headTitle($title);
 ?>
 <h1><?php echo $this->escapeHtml($title); ?></h1>
 <td><?php echo $this->escapeHtml($states->state);?></td>
 <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>
     <a href="/Blog/view/<?php echo $this->escapeHtml($posts->post_id);?>">
     <?php echo $this->escapeHtml($posts->post_title);?>
   </a>
 </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; ?>
 </table>  

添加表格12:01 EST 06/07/2017  邮政表:

 CREATE TABLE `posts` (
 `post_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `state_id` smallint(6) DEFAULT NULL,
 `created_user_id` int(11) DEFAULT NULL,
 `post_title` varchar(255) NOT NULL,
 `post_date` datetime DEFAULT NULL,
 `is_active` tinyint(4) NOT NULL,
 `status` enum('deleted','draft','inactive','active') DEFAULT 'inactive',
 `activate_date` datetime DEFAULT NULL,
 `is_hot` tinyint(4) DEFAULT '0',
 `ordering` int(11) DEFAULT '0',
 `num_views` int(11) DEFAULT '0',
 `allow_comment` tinyint(4) DEFAULT NULL,
 `num_comments` int(11) DEFAULT '0',
 `picLink` varchar(50) DEFAULT NULL,
 `description` varchar(1000) DEFAULT NULL,
 `food` enum('Yes',' No') DEFAULT NULL,
 PRIMARY KEY (`post_id`),
 KEY `idx_latest` (`status`,`activate_date`),
 KEY `idx_latest_category` (`state_id`,`status`,`activate_date`),
 KEY `idx_most_commented` (`state_id`,`status`,`num_comments`) USING BTREE,
 KEY `idx_most_viewed` (`state_id`,`status`,`num_views`) USING BTREE,
 KEY `idx_most_viewed_2` (`status`,`num_views`),
 KEY `idx_created_user` (`created_user_id`,`post_id`)

州表:

 CREATE TABLE `states` (
 `state_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `state` varchar(45) NOT NULL,
 PRIMARY KEY (`state_id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8;   

1 个答案:

答案 0 :(得分:0)

根据您的posts,您无需加入stateslist.phtml表格即可显示。你试图输出像<{1}}那样的状态,因此这完全是外部循环。因此,您需要获取一个对象,您可以根据需要输出状态。在$states->state模型中创建另一种方法以使其工作。请参阅以下

StatesTable

来自public function getStateById($state_id) { $state_id = (int) $state_id; $rowset = $this->tableGateway->select(array('state_id' => $state_id)); $row = $rowset->current(); if (!$row) { throw new \Exception("Could not find row $state_id"); } return $row; } 模型的getbystate($state_id)方法应该像上面所写的那样保持不变。 PostsTable方法应该是以下

listAction()

现在我告诉你的sql查询加入两个表是不对的,那就是这个

public function ListAction()
{
    $state_id = $this->params()->fromRoute('state_id');

    $list = $this->getPostsTable()->getbystate($state_id);
    $states = $this->getStatesTable()->getStateById($state_id);

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

    return $view;  
}

此方法有局限性。由于结果集原型问题,您无法将此结果集对象转换为数组。但这会以某种方式起作用。

相关问题