我还有另一个问题。我正在尝试加入两个表格,以便我可以从两者中提取数据。我是TableGateway的新手,并且遇到问题时发现相同的情况。我有一个页面,将按国家拉博客,我也有一个状态表。我想加入这些使用state_id并显示“状态”从页面上的状态表。以下是我的。不知道我是否使用正确的代码!为什么从联接声明中显示此错误1066
模式 - 从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;
}
更新8:41 EST 06/06/2017
我把连接放到Posts模块中而不是在States模型中。 我已经摆脱了错误,但我仍然需要知道如何显示状态。除了驻留在posts模块中,以上所有内容都是相同的。
“查看”位于 - 错误 注意:未定义的属性:Zend \ Db \ Adapter \ Driver \ Pdo \ Result :: $ state in/var/www/html/module/Blog/view/blog/list.phtml第6行
查看:
$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 2017年6月7日 帖子表:
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`)
国表:
你的sql查询加入两个表是不正确的。你能够显示你的“博客”和“状态”表吗? – unclexo
@ J.Sajeeb我已将表格添加到帖子中。也许索引也不完全正确。当我做了一个垃圾场时,我在所有的钥匙上都被淹没了。我不记得那么做。谢谢。 –