Blog tutorial (Part 2)

In part 1 we dealt with listing and adding posts to the blog. We used models, views and controllers to achieve this. Furthermore, we used the ORM and Formation library to aid us. In part 2 we’ll deal with relationships in ORM and adding comments to posts.

We first need a page to view a blog post. To do this we add another controller method named view_post(). Accessible through localhost/blog/view_post/post_id We need to link to this page so we first adapt the listing view.

The view (views/blog.php) will look like:

foreach($posts as $post)
{
	echo '<h1>'.html::anchor('blog/view_post/'.$post->id, $post->title).'</h1>';
	echo '<p>'.$post->text.'</p>';
}

You see I link to the view_post method. Now, we need to create the method in the controller. In your Blog_Controller add the following:

	public function view_post($id)
	{
		$post=ORM::factory('post',(int) $id);
 
		$view=new View('view_post');
		$view->post=$post;
		$view->comments=$post->find_related_comments();
		$view->render(true);
	}

The ORM retrieves the post given as argument, localhost/blog/view_post/2 will retrieve post with id = 2. I also request all comments related to this post. The post and the comments are then added to the view and the view is rendered.
The view file ‘views/view_post.php’ looks like this.

	echo html::anchor('blog','Post list') .'<hr>';
	echo '<h1>'. $post->title.'</h1>';
	echo '<p>'.$post->text.'</p>';
 
	echo '<p>'.html::anchor('blog/add_comment/'.$post->id, 'Add comment').'</p>';
	echo '<hr>';
	foreach($comments as $comment)
	{
		echo '<p><strong>'.$comment->name.'</strong><br />';
		echo ''.$comment->text.'</p>';
	}

I add a link at the top linking back to the post listing, and a link at the bottom to add comments. I also list all comments for this post. At present there should be none though.

Next step is adding the add_comment method. In your Blog_Controller

	public function add_comment($id)
	{
		$post=ORM::factory('post',(int) $id);
 
		$form=new Formation; //you can use Forge as well here
		$form->add_element('input','name')->add_rule('Rule_Required');
		$form->add_element('email','email')->add_rule('Rule_Required'); //email element has email rule attached
		$form->add_element('textarea','text')->add_rule('Rule_Required');
		$form->add_element('submit','Submit');
 
		if($form->validate())
		{
			$comment=new Comment_Model;
			$comment->load_values($form->as_array());
			$post->add_comment($comment);
 
			url::redirect('blog/view_post/'.(int) $id);	
		}
		else
		{
			$view=new View('add_post');
			$view->form=$form->render();
			$view->render(true);			
		}
	}

What’s happening. I first load the post the comments should be added to, then I put together the form for adding comments. If the form validates I create a Comment_Model and add it to the post model so as to create the relationship. If it doesn’t validate I show the form with the views/add_post.php view.

<h1>Add comment</h1>
<?php
echo $form;
?>

That’s it, you now have the basis for a blog. You (and everybody else) can add posts, posts are listed and can be viewed together with comments. Furthermore, people can add comments. I hope it was useful.


7 Responses to “Blog tutorial (Part 2)”

  1. Brian Says:

    How do you deal with form error messages?

  2. dlib Says:

    Libraries like forge and formation automatically deal with them.

  3. bapanajalu Says:

    Hi there,
    I’m new to kohana, and just finishing your blog tutorial.

    How do you display an image that has been uploaded by using forge ?

    I’ve manage to create an upload form by using Forge, but get stuck create a form that displaying image (this form is used to edit data).

    Can you give me direction ?

    Thanks
    bapanajalu

  4. Karsten Says:

    load_values didnt work for me in 2.2 here. what I did to the add_comment function to get it to work:

    public function add_comment($id)
    {
    $post=ORM::factory(’post’,(int) $id);
    $form=new Formation; //you can use Forge as well here
    $form->add_element(’input’,'name’)->add_rule(’Rule_Required’);
    $form->add_element(’email’,'email’)->add_rule(’Rule_Required’); //email element has email rule attached
    $form->add_element(’textarea’,'text’)->add_rule(’Rule_Required’);
    $form->add_element(’hidden’,'post_id’)->set_value($id);
    $form->add_element(’submit’,'Submit’);

    if($form->validate())
    {
    $comment=new Comment_Model;
    //$comment->load_values($form->as_array());
    $comment->name= (string) $form['name'];
    $comment->email= (string) $form['email'];
    $comment->text= (string) $form['text'];
    $comment->post_id = (string) $form['post_id'];
    $comment->post_id =
    $comment->save($comment);

    url::redirect(’blog/view_post/’.(int) $id);
    }

  5. Karsten Says:

    This line was a typo:

    “$comment->post_id =”

  6. hardtop Says:

    Nice tutorial. But how to delete one comment or whole post with related comments? This code doesn’t work:

    $comments=$post->find_related_comments();
    $comments->remove();

    Thanx!

  7. toulon Says:

    In order for this tutorial to work for the new version of ORM (kohana 2.2) you need to make the following changes:

    controllers/blog.php
    remove
    $view->comments = find_related_comments();

    Add
    $comment->post_id = $form['id'];
    $comment->save(); //Save comment
    after
    if ($form->validate()) { // If comment record is found and validated then display and allow for editing
    $comment= new Comment_Model;
    $comment->name = (string) $form['name'];
    $comment->email = (string) $form['email'];
    $comment->text = (string) $form['text'];

    replace
    } else {
    echo “blog->add_comment->validated “;
    $view = new View(’add_post’);
    $view->form=$form->render();
    $view->render(true);
    with
    } else {
    echo “blog->add_comment->validated “;
    $view = new View(’add_comment’);
    $view->form=$form->render();
    $view->render(true);

    Insert
    $form->add_element(’hidden’, ‘id’)->set_value($id);
    after
    public function add_comment($id)
    {
    //echo “blog->add_comment”;
    $post=ORM::factory(’post’,(int) $id);

    $form=new Formation;

    Create file views/add_comment.php
    Add comment
    view->add_post”;
    echo $form;
    ?>

    views/view_posts.php
    replace
    foreach($comments as $comment) {
    echo ‘‘.$comment->name.’‘;
    echo $comment->text.”;
    }
    with
    foreach ($post->comments as $comment) {
    echo “blog->view->view_post->foreach->post->comments”;
    echo ‘‘.$comment->name.’‘;
    echo $comment->text.”;
    }

Leave a Comment