Передача переменной из вида в модель в 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 комментария равно 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_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 } ?> 

Сообщение об ошибке

Попытка получить свойство не-объекта ($ comment) в представлении

Результат для var_dump ($ comments); в индексной функции в контроллере (у меня есть один комментарий и 2 сообщения (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