我还有另一个问题。我正在尝试加入 2 个表,以便我可以从这两个表中提取数据。我是 TableGateway 的新手,遇到相同情况时遇到问题。我有一个按州拉博客的页面,我也有一个状态表。我想使用 state_id 加入这些并从页面上的状态表中显示“状态”。下面是我所拥有的。不确定我是否使用了正确的代码!
模型 - 从状态表:
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 Model 中。我已经摆脱了错误,但是我仍然需要知道如何显示状态。上面的一切都是一样的,只是它驻留在帖子模块中。
“视图”如下 - 它错误注意:未定义的属性: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 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;