将变量从视图传递到MVC中的模型 [英] Pass a variable from view to model in mvc

查看:76
本文介绍了将变量从视图传递到MVC中的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立评论系统.

数据库结构

I am trying to build a comment system.

The database structure

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等于帖子的post_id.

模型


The Goal
I would like to display just those comments, where the post_id of the comment is equal to the posts' post_id.

The Model

    /**
     * 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();
    }


控制器


The Controller

    public function index()
    {   
        $this->View->render('index/index', array(
            'posts' => PostModel::getAllPosts(),
            'comments' => CommentModel::getComments($post_id)
        ));
    }


视图


The View

    <!-- 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_id以将其与评论链接?

任何帮助将不胜感激!谢谢!


更新

控制器



The problem
The $post_id variable is undefined. How can I get the post_id of every post to link them with the comments?

Any help would be highly appreciated! Thank you!


UPDATE

The Controller

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
        ));
    }


视图


The View

<?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 } ?>


错误消息


Error Message

试图在视图中获取非对象($ comment)的属性

Trying to get property of non-object ($comment) in the View


输出,用于var_dump($ comments);在控制器的索引函数中(我有一条评论和2条帖子(post_id = 22和15)):


The output for var_dump($comments); in the index function in the controller (I have one comment and 2 posts (post_id= 22 and 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)的输出;在视图中:

The output for var_dump($comments); in the view:

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;
    ));
}

然后在您看来,只需执行

Then in your view, just do a

foreach($ this-> comments [$ post-> post_id] as $ comment)

foreach($this->comments[$post->post_id] as $comment)

以显示与帖子相关的所有评论.

in order to display all comments associated to a post.

这篇关于将变量从视图传递到MVC中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆