将变量从视图传递到 mvc 中的模型
Pass a variable from view to model in mvc
我正在尝试建立一个评论系统。
数据库结构
TABLE `posts` (
`post_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`post_text` text NOT NULL,
`user_id` int(11) NOT NULL,
`post_creation` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
TABLE `comments` (
`comment_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`comment_text` text NOT NULL,
`comment_creation` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL,
`post_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`comment_id`),
FOREIGN KEY (`post_id`) REFERENCES posts(post_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
目标
我只想显示那些评论,其中评论的 post_id 等于 posts' post_id.
模特
/**
* Get all comments from a certain post
* @return array an array with several objects (the comments)
*/
public static function getComments($post_id)
{
$database = DatabaseFactory::getFactory()->getConnection();
$sql = "SELECT * FROM comments WHERE post_id = :post_id";
$query = $database->prepare($sql);
$query->execute(array(':post_id' => $post_id));
return $query->fetchAll();
}
控制器
public function index()
{
$this->View->render('index/index', array(
'posts' => PostModel::getAllPosts(),
'comments' => CommentModel::getComments($post_id)
));
}
景色
<!-- Display posts -->
<?php if ($this->posts) { ?>
<?php foreach($this->posts as $post) { ?>
<?= htmlentities($post->post_text); ?><br>
<?php } ?>
<!-- Comment section -->
<small>
<form method="post" action="<?php echo Config::get('URL');?>comment/create">
<input type="hidden" name="post_id" value="<?php echo htmlentities($post->post_id); ?>" />
<input type="text" name="comment_text" placeholder="comment..." />
<input type="submit" value='comment' autocomplete="off" />
</form><br>
<?php if ($this->comments) { ?>
<?php foreach($this->comments as $comment) { ?>
<?= htmlentities($comment->comment_text); ?><br>
<?php } ?>
<?php } ?>
</small><br><br>
<?php } ?>
<?php } else { ?>
<div>Get some friends to view posts.</div>
<?php } ?>
问题
$post_id 变量未定义。我怎样才能通过评论获得每个 post 到 link 的 post_id?
任何帮助将不胜感激!谢谢!
更新
控制器
public function index()
{
$posts = PostModel::getAllPosts();
$comments = array();
foreach ($posts as $post) {
$comments[$post->post_id][] = CommentModel::getComments($post->post_id);
}
$this->View->render('index/index', array(
'posts' => $posts,
'comments' => $comments
));
}
景色
<?php foreach($this->comments[$post->post_id] as $comment) { ?>
<?= htmlentities($comment->comment_text); ?><br>
<?php if ($comment->user_id == Session::get('user_id')) { ?>
<small><a href="<?= Config::get('URL') . 'comment/delete/' . $comment->comment_id; ?>">Löschen</a></small><br>
<?php } ?>
<?php } ?>
错误信息
Trying to get property of non-object ($comment) in the View
var_dump($comments) 的输出;在控制器的索引函数中(我有一个评论和 2 posts(post_id= 22 和 15)):
array(2) {
[22]=> array(1) {
[0]=> array(1) {
[0]=> object(stdClass)#8 (5) {
["comment_id"]=> string(1) "1"
["comment_text"]=> string(17) "this is a comment"
["comment_creation"]=> string(19) "2015-02-28 19:44:09"
["user_id"]=> string(1) "1"
["post_id"]=> string(2) "22" } } }
[15]=> array(1) { [0]=> array(0) { } } }
var_dump($comments) 的输出;在视图中:
NULL
尝试这样的事情:
public function index()
{
$posts = PostModel::getAllPosts();
$comments = array();
foreach ($posts as $post)
{
$comments[$post->post_id] = CommentModel::getComments($post->post_id);
}
$this->View->render('index/index', array(
'posts' => $posts,
'comments' => $comments;
));
}
那么在你看来,做一个
foreach($this->comments[$post->post_id] as $comment)
以便显示与 post 关联的所有评论。
我正在尝试建立一个评论系统。
数据库结构
TABLE `posts` (
`post_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`post_text` text NOT NULL,
`user_id` int(11) NOT NULL,
`post_creation` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
TABLE `comments` (
`comment_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`comment_text` text NOT NULL,
`comment_creation` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`user_id` int(11) NOT NULL,
`post_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`comment_id`),
FOREIGN KEY (`post_id`) REFERENCES posts(post_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
目标
我只想显示那些评论,其中评论的 post_id 等于 posts' post_id.
模特
/**
* Get all comments from a certain post
* @return array an array with several objects (the comments)
*/
public static function getComments($post_id)
{
$database = DatabaseFactory::getFactory()->getConnection();
$sql = "SELECT * FROM comments WHERE post_id = :post_id";
$query = $database->prepare($sql);
$query->execute(array(':post_id' => $post_id));
return $query->fetchAll();
}
控制器
public function index()
{
$this->View->render('index/index', array(
'posts' => PostModel::getAllPosts(),
'comments' => CommentModel::getComments($post_id)
));
}
景色
<!-- Display posts -->
<?php if ($this->posts) { ?>
<?php foreach($this->posts as $post) { ?>
<?= htmlentities($post->post_text); ?><br>
<?php } ?>
<!-- Comment section -->
<small>
<form method="post" action="<?php echo Config::get('URL');?>comment/create">
<input type="hidden" name="post_id" value="<?php echo htmlentities($post->post_id); ?>" />
<input type="text" name="comment_text" placeholder="comment..." />
<input type="submit" value='comment' autocomplete="off" />
</form><br>
<?php if ($this->comments) { ?>
<?php foreach($this->comments as $comment) { ?>
<?= htmlentities($comment->comment_text); ?><br>
<?php } ?>
<?php } ?>
</small><br><br>
<?php } ?>
<?php } else { ?>
<div>Get some friends to view posts.</div>
<?php } ?>
问题
$post_id 变量未定义。我怎样才能通过评论获得每个 post 到 link 的 post_id?
任何帮助将不胜感激!谢谢!
更新
控制器
public function index()
{
$posts = PostModel::getAllPosts();
$comments = array();
foreach ($posts as $post) {
$comments[$post->post_id][] = CommentModel::getComments($post->post_id);
}
$this->View->render('index/index', array(
'posts' => $posts,
'comments' => $comments
));
}
景色
<?php foreach($this->comments[$post->post_id] as $comment) { ?>
<?= htmlentities($comment->comment_text); ?><br>
<?php if ($comment->user_id == Session::get('user_id')) { ?>
<small><a href="<?= Config::get('URL') . 'comment/delete/' . $comment->comment_id; ?>">Löschen</a></small><br>
<?php } ?>
<?php } ?>
错误信息
Trying to get property of non-object ($comment) in the View
var_dump($comments) 的输出;在控制器的索引函数中(我有一个评论和 2 posts(post_id= 22 和 15)):
array(2) {
[22]=> array(1) {
[0]=> array(1) {
[0]=> object(stdClass)#8 (5) {
["comment_id"]=> string(1) "1"
["comment_text"]=> string(17) "this is a comment"
["comment_creation"]=> string(19) "2015-02-28 19:44:09"
["user_id"]=> string(1) "1"
["post_id"]=> string(2) "22" } } }
[15]=> array(1) { [0]=> array(0) { } } }
var_dump($comments) 的输出;在视图中:
NULL
尝试这样的事情:
public function index()
{
$posts = PostModel::getAllPosts();
$comments = array();
foreach ($posts as $post)
{
$comments[$post->post_id] = CommentModel::getComments($post->post_id);
}
$this->View->render('index/index', array(
'posts' => $posts,
'comments' => $comments;
));
}
那么在你看来,做一个
foreach($this->comments[$post->post_id] as $comment)
以便显示与 post 关联的所有评论。