Kohana Tutorial: Part 2

////Kohana Tutorial: Part 2

Kohana Tutorial: Part 2

Here is the 2nd part of 3 part series of Kohana tutorial. First part of this tutorial can be found here as Kohana Tutorial: For the beginners. Same as before it is going to be 10+/- steps.

[framed_box]
This part is going to cover

  • Template Controller in Kohana
  • Kohana ORM (relation & validation)

[/framed_box]

Why Template Controller?

Its tedious and repetitive task to create and manage all the view files with header, footer, menu… etc all the time. So it could be better, if we had a common template to handle this kind of stuffs. Kohana have Template Controller to rescue you.

Lets create a template

We want that, our website will have common pattern with header, menu, css etc. We need to create a simple template file like as below.

[php title=”kohana-blog/application/views/template.php”]
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Kohana Blog</title>
<!– this is how, you you can add a css file –>
<?php echo HTML::style("public/css/template.css"); ?>
<!– this is how, you you can add a javascript file –>
<?php echo HTML::script("public/js/jquery-1.6.4.js"); ?>
</head>

<body>
<div id="wrapper">
<!– Header for the site –>
<div id="header"><h1>Kohana Blog</h1></div>

<!– Menu for the site –>
<div id="navlist">
<ul>
<li><?php echo HTML::anchor("", "Home"); ?></li>
<li><?php echo HTML::anchor("article", "Articles"); ?></li>
<li><?php echo HTML::anchor("article/edit", "New Article"); ?></li>
</ul>
</div>

<!– here we load all content –>
<div id="content">
<?php echo $content; ?>
</div>

<!– no footer added –>
</div>
</body>
</html>
[/php]

Working with template controller

Now we need to say our controller that, from now on you are going to handle the template yourself. To do this we need to change our article controller. We can do it in following way.

[framed_box]

  1. Extend Controller_Article from Controller_Template instead of Controller 😉
  2. Set the template for your controller as $template = “template”, since our template file is template.php in view folder.
  3. For all actions populate the $template as highlighted below at line 12. Do it for all other methods where required.

[/framed_box]

[php title=”kohana-blog/application/classes/controller/article.php” highlight=”2,4,12″]
// extends the Controller_Template
class Controller_Article extends Controller_Template {

public $template = ‘template’;

public function action_index() {
$articles = ORM::factory(‘article’)->find_all(); // loads all article object from table

$view = new View(‘article/index’); // load ‘article/index.php’ view file
$view->set("articles", $articles); // set "articles" object to view

$this->template->set(‘content’, $view); // renders a view as a response
}

}
[/php]

It should be fun and easy to do this. Now hit the url: http://localhost/kohana-blog/index.php/article and see the magic. We can see a nice look of our Kohana Blog.

Working with ORM

Already you got some idea on how ORM works if you have practiced Part 1. Now its time to do something few more with ORM. Lets play with ORM relation & validation in Kohana.

Create comment model

How about adding comments to all these articles. Use the following script to create a comments table.

[sql]
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`article_id` int(10) NOT NULL,
`comment` varchar(45) NOT NULL,
`time` datetime NOT NULL,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_comments_article` (`article_id`)
);
[/sql]

  • And below we see the Comment Model.

[php title=”kohana-blog/application/classes/model/comment.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);
class Model_Comment extends ORM {

}
[/php]

Now add relation to models

[framed_box]
An article can have multiple comment. That means one-to-many relation. This is how we can do it using Kohana ORM.
[/framed_box]

[php highlight=”6,7-12″ title=”kohana-blog/application/classes/model/article.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Model_Article extends ORM {

// contains many relations
protected $_has_many = array(
// an article has many comments
‘comments’ => array(
‘model’ => ‘comment’,
‘foreign_key’ => ‘article_id’,
),
);

}
[/php]
Now something like $article->comments will allow us to get all comments for an article 😉

[framed_box]
And a comment can be related to only one article. So it is many-to-one relation. Below shows, how we can do it using Kohana ORM.
[/framed_box]
[php highlight=”6,7-12″ title=”kohana-blog/application/classes/model/comment.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Model_Comment extends ORM {

// contains many to one relation mainly
protected $_belongs_to = array (
// a comment is related to only one article
‘article’ => array (
‘model’ => ‘article’,
‘foreign_key’ => ‘article_id’
)
);
}
[/php]
Now something like $comment->article will allow us to get article related to a comment 😉

We wont be able to see any big difference on how ORM is working here, but later part of the discussion should give some hints.

Some improvement

Before going further, lets show a single article first. It should be simple as follows.

  • Create an action to load the article

[php title=”kohana-blog/application/classes/controller/article.php”]
class Controller_Article extends Controller_Template {

public function action_view() {
$article_id = $this->request->param(‘id’);
$article = ORM::factory(‘article’, $article_id);
$view = new View(‘article/single’);
$view->set("article", $article);

$this->template->set(‘content’, $view);
}

}
[/php]

  • Create a view file to show the article

[php title=”kohana-blog/application/views/article/single.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’); ?>

<div>
<h2><?php echo $article->title; ?></h2>
<pre><?php echo $article->content; ?></pre>
<?php echo HTML::anchor("article/edit/".$article->id, "Edit"); ?>
<?php echo HTML::anchor("article/delete/".$article->id, "Delete"); ?>
</div>
[/php]

Now it is possible to view an article in a single page. We can try opening a url like: http://localhost/kohana-blog/index.php/article/view/1

Add comments to an article

Now we will add comments to an article. Create an edit form & a Controller_Comment to save the comment for a certain article. These are the changes we need to do.

  • Create the comment edit form

[php title=”kohana-blog/application/views/comment/edit.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’); ?>

<h3>Add your comment</h3>

<?php echo Form::open(‘comment/post/’); ?>
<?php echo Form::label("name", "Name"); ?>
<br />
<?php echo Form::input("name", $comment->name); ?>
<br />
<br />
<?php echo Form::label("email", "Email"); ?>
<br />
<?php echo Form::input("email", $comment->email); ?>
<br />
<br />
<?php echo Form::label("comment", "Comment"); ?>
<br />
<?php echo Form::textarea("comment", $comment->comment); ?>
<br />
<br />
<?php echo Form::hidden("article_id", $article->pk()); ?>
<?php echo Form::submit("submit", "Submit"); ?>
<?php echo Form::close(); ?>
[/php]

  • Load the form from article view page

[php highlight=”10,11″ title=”kohana-blog/application/views/article/single.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’); ?>

<div>
<h2><?php echo $article->title; ?></h2>
<pre><?php echo $article->content; ?></pre>
<?php echo HTML::anchor("article/edit/".$article->id, "Edit"); ?>
<?php echo HTML::anchor("article/delete/".$article->id, "Delete"); ?>
</div>

<!– this practice should be preferable, instead of cluttering a single article page with everything –>
<?php echo View::factory(‘comment/edit’, array(‘comment’=>new Model_Comment(),’article’=>$article )); ?>
[/php]

  • Create the action to save the comment in Comment Controller

[php title=”kohana-blog/application/classes/controller/comment.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Controller_Comment extends Controller {

public function action_post() {
$comment = new Model_Comment();
$comment->values($this->request->post());
$comment->save();

$this->redirect("index.php/article/view/".$comment->article_id);
}

}
[/php]

Now try making some comments to one of the article. We have not shown the comments yet, so you should check them in your database 😉

Show article comments

At first, we create a view file to show a single comment.
[php title=”kohana-blog/application/views/comment/single.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’); ?>

<div class="comment">
<span><?php echo $comment->name; ?></span>
<pre><?php echo $comment->comment; ?></pre>
</div>
[/php]

Article’s view page can show all these comments as shown below. $article->comments should be interesting. It refers to comments related to an article.
[php firstline=”9″ highlight=”10,11-15″ title=”kohana-blog/application/views/article/single.php”]

<div id="comments" style="margin: 10px 0px 10px 0px">
<?php foreach ($article->comments->find_all() as $comment) : ?>
<!– showing a single comment –>
<?php echo View::factory(‘comment/single’, array(‘comment’=>$comment)); ?>
<?php endforeach; ?>
</div>

<!– this practice should be preferable, instead of cluttering a single article page with everything –>
<?php echo View::factory(‘comment/edit’, array(‘comment’=>new Model_Comment(),’article’=>$article )); ?>
[/php]
Here $article->comments should be easily understood. I am not gonna explain it 😉

So far, so good. Now we hit the url: http://localhost/blog/index.php/article/view/1 and see all those comments.

Working with ORM validation

How about trying with validation in Kohana ORM. It will be nice if we could validate model data each time before saving to database. Lets see, how we can do it with Kohana ORM.

But don’t be confused, I am just focusing on validating a model object. Using Kohana Validation it is possible to validate any other things also.

[php highlight=”14,15-32″ title=”kohana-blog/application/classes/model/article.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’);

class Model_Article extends ORM {

// contains many relations
protected $_has_many = array(
// an article has many comments
‘comments’ => array(
‘model’ => ‘comment’,
‘foreign_key’ => ‘article_id’,
),
);

/**
* Rule definitions for validation
*
* @return array
*/
public function rules() {
return array (
‘title’ => array (
array(‘not_empty’),
),
‘content’ => array ( // property name to validate
array(‘not_empty’), // validation type
array(
‘min_length’, // validation type
array(‘:value’, 10) // validation parameters
),
),
);
}

}
[/php]
Now try creating some article with invalid data, and you will see ORM_Validation_Exception. We need to show these error messages. So we need to catch this exception in while saving the article and show these error messages in article edit form. Here we go…
[php highlight=”10,11-23″ title=”kohana-blog/application/classes/controller/article.php”]
class Controller_Article extends Controller_Template {

// save the article
public function action_post() {
$article_id = $this->request->param(‘id’);
$article = new Model_Article($article_id);
$article->values($this->request->post()); // populate $article object from $_POST array
$errors = array();

try {
$article->save(); // saves article to database
$this->redirect(self::INDEX_PAGE);
} catch (ORM_Validation_Exception $ex) {
$errors = $ex->errors(‘validation’);
}

$view = new View(‘article/edit’);
$view->set("article", $article);
$view->set(‘errors’, $errors);

$this->template->set(‘content’, $view);
}

}
[/php]

  • Update the form to show error message

[php highlight=”11,17″ title=”kohana-blog/application/views/article/edit.php”]
<?php defined(‘SYSPATH’) or die(‘No direct script access.’); ?>

<h1><?php echo ($article->id? "Edit" : "New"); ?> article</h1>

<?php $errors = isset($errors) ? $errors : array(); ?>

<?php echo Form::open(‘article/post/’.$article->id); ?>
<?php echo Form::label("title", "Title"); ?>
<br />
<?php echo Form::input("title", $article->title); ?>
<span class="error"><?php echo Arr::get($errors, ‘title’);?></span>
<br />
<br />
<?php echo Form::label("content", "Content"); ?>
<br />
<?php echo Form::textarea("content", $article->content); ?>
<span class="error"><?php echo Arr::get($errors, ‘content’);?></span>
<br />
<br />
<?php echo Form::submit("submit", "Submit"); ?>
<?php echo Form::close(); ?>
[/php]
Now try creating an article with invalid data here: http://localhost/kohana-blog/index.php/article/edit. Check if the error messages are shown correctly 🙂

Bonus! Show label in validation

It should be all. But wait, still one more hints. It should be nice if we could see “title” instead of “title” or “Content” instead of “content” in error message. We can do it by following way. Create a function label which will return label for all the fields of a model.

[php highlight=”3,4-8″ title=”kohana-blog/application/classes/model/article.php”]
class Model_Article extends ORM {

public function labels() {
return array(
‘title’ => ‘Title’,
‘content’ => ‘Content’,
);
}

}
[/php]
Now we will see nice error message with their label with it.

[framed_box]
This is all for today :). I have tried to give some basic idea on…

  • Template Controller on Kohana
  • Kohana ORM: Working with relation
  • Kohana ORM: Working with validation
  • Organize view files, if you have noticed how I have organized view files

[/framed_box]


Downloads: https://bitbucket.org/kowsercse/kohana-blog
Part 1: Kohana Tutorial: For the beginners
More on Kohana: http://kerkness.ca/kowiki/doku.php

By | 2014-03-05T23:38:44+00:00 November 4th, 2011|kohana, php|41 Comments

About the Author:

2 hand 2 leg 2 ear and 2 eyes... i am a man who is living to live...

41 Comments

  1. Jorge November 7, 2011 at 10:18 PM - Reply

    Hi there 🙂

    I’ve just started learning kohana 3 and this is by far the best and most up to date tutorial out there.

    I’ve also found a book from Packt Publishing by Jason D. Straughan, Kohana 3.0 Beginner’s Guide. Even although the book is only 4 months old the code is outdated, which is very discourage for beginners like me.

    Thanks a lot for sharing your ideas. Hope to see more from you.

    Greetings from Spain 🙂

  2. Bilash Saha November 8, 2011 at 10:12 PM - Reply

    Hi,

    Great tutorial !!!

    Hope to see more.

    Carry on.

    • fg October 17, 2012 at 3:56 PM - Reply

      lllllllllllllll

  3. Sinan E. November 23, 2011 at 11:59 PM - Reply

    I loved your post. Learned so many things.
    Thanks you very much.

    • Kowser November 24, 2011 at 8:27 AM - Reply

      Glad, that you liked it so much.

  4. James January 8, 2012 at 2:30 PM - Reply

    Hi thanks for the great tutorial I have one issue with the first part .
    I get error message “kohana_Exception [0]: the content property does not exist in the Model_Article class”.
    Can you ple
    ase help me figure out what’s wrong? Thank you so much.

    • Jouanne Jimmy January 27, 2013 at 2:51 PM - Reply

      Hi James,
      I got this error and it’s our fault because we don’t put the good thing in the good file.
      Here is the solution :

      // application/classes/Model/Article.php
      class Model_Article extends ORM {
      // contains many relations
      protected $_has_many = array(
      // an article has many comments
      ‘comments’ => array(
      ‘model’ => ‘comment’,
      ‘foreign_key’ => ‘article_id’,
      ),
      );
      }

      and the second model comment

      // application/classes/Model/Comment.php
      class Model_Comment extends ORM {
      // contains many to one relation mainly
      protected $_belongs_to = array (
      // a comment is related to only one article
      ‘article’ => array (
      ‘model’ => ‘article’,
      ‘foreign_key’ => ‘article_id’
      )
      );
      }

      It’s two different code which not contains on the same file.

  5. Abdullah Al Mamun January 24, 2012 at 7:08 PM - Reply

    Thanks a lot. 🙂
    Really, a brilliant series for kohana newbies.
    Hope, on next part you will focus on authentication.

  6. Jeremy March 10, 2012 at 2:31 PM - Reply

    When is part 3 due?

  7. S van Boxel March 14, 2012 at 4:50 AM - Reply

    Still waiting for part 3… I can use it. 😉

  8. Lynks March 21, 2012 at 6:17 AM - Reply

    Great portion of knowledge – all I know about KOHANA, is thanks you 🙂
    I hope you will publish 3rd part of your series.

    Greetings from Poland

  9. T.G April 7, 2012 at 3:15 PM - Reply

    Very useful article!

    wrong filename: kohana-blog/application/views/article/edit.php this is comment/edit.php

    • Kowser April 7, 2012 at 8:18 PM - Reply

      Thanks for pointing the issue. And glad to know it was helpful for you.

  10. venkatraja April 18, 2012 at 7:00 PM - Reply

    how to set id,class in input format in kohana

  11. Daka April 27, 2012 at 2:01 PM - Reply

    What about database injection xxs attack etc.
    Does ORM really clean it up, so we don’t need to do that?
    like if we using database we will say Database::instance()->escape($string)
    is it by ORM different?

  12. Robert May 4, 2012 at 7:39 PM - Reply

    Hi,
    Just writing to encourage you to do some more tutorials. I´m also new to kohana and find tutorials like the ones you have written which are task-based much more valuable than formal definitions and abstract explainations.

    I hope you are planning to write many more soon,

    yours,

    Rob

  13. Robert May 7, 2012 at 7:31 PM - Reply

    Hi,

    I´m having a problem connecting to the database. I dapated database.sql for my username and password, but when I go to http://MyDomain/kohana/article/ I just get this…
    “Database_Exception [ 2 ]: mysql_connect(): Access denied for user ‘www-data’@’localhost’ (using password: NO)”

    can you tell me what´s going wrong?

    thanks,

    Rob

    • mark May 14, 2012 at 5:10 AM - Reply

      hi rob,

      check your username and database name in config/database.php the error is showing the script can’t connect to the database with that login information

  14. reddy May 16, 2012 at 2:21 AM - Reply

    this tutorial is very helpful and hope you can continue writing the series with full kohana tutorial.

  15. an0nemous June 1, 2012 at 9:54 PM - Reply

    Great tutorial, thanks!! However, I do have one question, why not just re-use the edit action in action_post to display the error. Seems more like code repetition to me. Could you not simply pass the errors to that function? Thanks.

  16. fabiyi adeolu August 19, 2012 at 1:09 AM - Reply

    I should say tttttthankssssss so much for the tutorials you have really blessed my life. You are lifted.Greetings from Nigeria

  17. Jason McCarrell August 23, 2012 at 8:45 AM - Reply

    Great article.

    FYI you have an error in the factory portion of the comments. You can’t set the variables through the factory. I don’t know why, but according to the user guide you need to simply chain set and use that.

    The variable simply does not end up being defined.

    You should definitely do part 3. Well written :).

  18. Dinesh October 8, 2012 at 1:27 PM - Reply

    This is a very nice ORM tutorial, Thanks a lot, Please continue with more tutorials..

    I have some doubts : If we are adding a same article title, it is updating to table. I want to check whether the article is already existing in table. I have added as in Model class:-

    public function rules() {
    return array (
    ‘title’ => array (
    array(‘not_empty’),

    array(‘min_length’, array(‘:value’, 4)),
    array(‘max_length’, array(‘:value’, 48)),
    array(array($this, ‘title_notavailable’)),
    ),

    public function title_notavailable($title)
    {

    return !(bool) DB::select(‘title’)
    ->from($this->_table_name)
    ->where(‘title’, ‘=’, $title)
    ->execute();

    }

    It is not validating properly!..

    Please suggest a good solution

    • arumdev April 21, 2013 at 4:42 PM - Reply

      I’m having trouble getting the templates working, would appreciate some help.

      it all works fine with (from the first part of your tutorial:

      class Controller_Article extends Controller {
      const INDEX_PAGE = “article”; // just give the name of Controller..
      public $template = ‘template’; // initiate the template variable

      public function action_index() {
      $articles = ORM::factory(‘article’)->find_all(); // loads all article object from table

      $view = new View(‘article/index’);
      $view->set(“articles”, $articles); // set “articles” object to view

      $this->response->body($view); // render the view as response
      }

      but when I make the changes:

      class Controller_Article extends Controller_Template { // make sure extends correct class
      const INDEX_PAGE = “article”; // just give the name of Controller..
      public $template = ‘template’; // initiate the template variable

      public function action_index() {
      $articles = ORM::factory(‘article’)->find_all(); // loads all article object from table

      $view = new View(‘article/index’);
      $view->set(“articles”, $articles); // set “articles” object to view

      $this->template->set(‘content’, $view); // renders a view as a response
      }

      I get the following error (in fact I can remove all the code inside the “class Controller_Article extends Controller” and I get the exact same error):

      View_Exception [ 0 ]: The requested view template could not be found
      SYSPATH/classes/Kohana/View.php [ 257 ]
      252 */
      253 public function set_filename($file)
      254 {
      255 if (($path = Kohana::find_file(‘views’, $file)) === FALSE)
      256 {
      257 throw new View_Exception(‘The requested view :file could not be found’, array(
      258 ‘:file’ => $file,
      259 ));
      260 }
      261
      262 // Store the file path locally
      SYSPATH/classes/Kohana/View.php [ 137 ] » Kohana_View->set_filename(arguments)
      SYSPATH/classes/Kohana/View.php [ 30 ] » Kohana_View->__construct(arguments)
      SYSPATH/classes/Kohana/Controller/Template.php [ 33 ] » Kohana_View::factory(arguments)
      SYSPATH/classes/Kohana/Controller.php [ 69 ] » Kohana_Controller_Template->before()
      {PHP internal call} » Kohana_Controller->execute()
      SYSPATH/classes/Kohana/Request/Client/Internal.php [ 97 ] » ReflectionMethod->invoke(arguments)
      SYSPATH/classes/Kohana/Request/Client.php [ 114 ] » Kohana_Request_Client_Internal->execute_request(arguments)
      SYSPATH/classes/Kohana/Request.php [ 990 ] » Kohana_Request_Client->execute(arguments)
      DOCROOT/index.php [ 118 ] » Kohana_Request->execute()

      my application directory looks like this:

      bootstrap.php
      cache/
      classes/
      Controller/
      article.php
      Welcome.php
      Model/
      article.php
      config
      i18n
      logs
      messages
      views/
      article/
      edit.php
      index.php
      template.php

      any help would be so appreciated as I’m still very new to kohana and MVC and even OOP in general.

      • arumdev April 21, 2013 at 5:07 PM - Reply

        Sorry please ignore my question, posted in the wrong place and also I fixed it, template file was one level too far down… duh

  19. John December 25, 2012 at 9:56 PM - Reply

    As an old man learning something new, I find your tutorial very interesting. My only problem is that in testing, all was okay until the program was complete and I receive the following error:

    ]
    28
    29 **** error ****
    30

    If you could kindly point me in the correct direction, I would greatly appreciate it.

  20. John December 28, 2012 at 5:15 AM - Reply

    referenced problem solved.

  21. Mans abbas January 14, 2013 at 2:11 PM - Reply

    Even i tried to fix all code errors but still didn’t work my form page, when click “New article” Link its shown this error,
    ErrorException [ Notice ]: Undefined variable: content

    why this error..?? i think any small code errors,

    thanks

    • wuwu February 18, 2013 at 4:05 AM - Reply

      add this :
      “$this->template->set(‘content’, $view);”
      at the end of method action_new()

  22. mahamood March 18, 2013 at 5:36 PM - Reply

    hi,
    good job. tk u. there are so many frameworks available. i hope kohana is really optimum.

  23. Admiral April 11, 2013 at 1:04 PM - Reply

    This is by far the best HOWTO in Kohana I’ve seen. Thank you for giving a piece of your brain.

    Greetings from the Davao City, Philippines

  24. arumdev April 21, 2013 at 6:20 PM - Reply

    Ok, really good tutorial once again. A few problems I came across:

    ‘redirect(self::INDEX_PAGE);’ does not work in 3.3, change to ‘HTTP::redirect(self::INDEX_PAGE);’ (remove single quotes of course).

    Also, the second-to-last step “Update the form to show error message” left the problem where if the new article page is loaded fresh the errors aren’t defined:

    ‘ErrorException [ Notice ]: Undefined variable: errors’ etc

    so instead of


    I added:


    hope that helps anyone getting the same errors. It’s kinda obvious but took my a while to pinpoint the problem.

    • arumdev April 21, 2013 at 6:21 PM - Reply

      ok your blog didn’t like the code. you just need to add a check to see if $errors is set:

      if(isset($errors)){echo Arr::get($errors, ‘title’);}

  25. kohana 3.3 blog tutorial parte 2 June 29, 2013 at 10:55 PM - Reply

    […] Come nella puntata precedente ringraziamo a Kowser autore del bellissimo articolo link, passimo ora a presentare la seconda parte, il post originale potete trovarlo seguendo il seguente link […]

  26. Jordan January 6, 2014 at 8:53 AM - Reply

    Your tutorial was great to follow. Just a few things that were missing that I would hope you could help me with..

    When trying to post a comment this error comes up:
    Database_Exception [ 1364 ]: Field ‘time’ doesn’t have a default value

    What line of code and which file do we have to edit. so that it puts a time stamp in the Time field in the Database?

  27. steven March 5, 2014 at 10:00 PM - Reply

    For users of version 3.3.1, the ‘Some Improvement’ section, I made the following change in kohana-blog/application/classes/controller/article.php to get this to work:
    Changed line
    $view = new View(‘article/single’);
    to
    $view = new View(‘article/single’, array($article_id));

  28. steven March 5, 2014 at 10:19 PM - Reply

    For users of version 3.3.1, the ‘Add comments to an article’ section, I made the following change in kohana-blog/application/classes/controller/comment.php to get this to work:
    Changed line
    $this->request->redirect(“index.php/article/view/” . $comment->article_id);
    to
    $this->redirect(“/article/view/” . $comment->article_id);

  29. steven March 5, 2014 at 10:59 PM - Reply

    For users of version 3.3.1, the ‘Working with ORM validation’ section, I made the following change in
    kohana-blog/application/views/article/edit.php
    from following two lines

    to

    Thanks to arumdev for already noting this fix.

    • steven March 5, 2014 at 11:29 PM - Reply

      Woops – same issue arumdev had — removes html from posts.
      Within ‘span’ tags the kohana-blog/application/views/article/edit.php changes noted, there should be a check as follows
      if(isset($errors)){echo Arr::get($errors, ‘title’);}

  30. firman June 18, 2015 at 2:58 PM - Reply

    Database_Exception [ 8192 ]: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

    MODPATH\database\classes\kohana\database\mysql.php [ 67 ]

    62 catch (Exception $e)
    63 {
    64 // No connection exists
    65 $this->_connection = NULL;
    66
    67 throw new Database_Exception(‘:error’,
    68 array(‘:error’ => $e->getMessage()),
    69 $e->getCode());
    70 }
    71
    72 // \xFF is a better delimiter, but the PHP driver uses underscore

    MODPATH\database\classes\kohana\database\mysql.php [ 171 ] » Kohana_Database_MySQL->connect()

    MODPATH\database\classes\kohana\database\mysql.php [ 360 ] » Kohana_Database_MySQL->query(arguments)

    MODPATH\orm\classes\kohana\orm.php [ 1504 ] » Kohana_Database_MySQL->list_columns(arguments)

    MODPATH\orm\classes\kohana\orm.php [ 392 ] » Kohana_ORM->list_columns(arguments)

    MODPATH\orm\classes\kohana\orm.php [ 337 ] » Kohana_ORM->reload_columns()

    MODPATH\orm\classes\kohana\orm.php [ 246 ] » Kohana_ORM->_initialize()

    MODPATH\orm\classes\kohana\orm.php [ 37 ] » Kohana_ORM->__construct(arguments)

    APPPATH\classes\controller\article.php [ 11 ] » Kohana_ORM::factory(arguments)

    {PHP internal call} » Controller_Article->action_index()

    SYSPATH\classes\kohana\request\client\internal.php [ 118 ] » ReflectionMethod->invoke(arguments)

    SYSPATH\classes\kohana\request\client.php [ 64 ] » Kohana_Request_Client_Internal->execute_request(arguments)

    SYSPATH\classes\kohana\request.php [ 1138 ] » Kohana_Request_Client->execute(arguments)

    DOCROOT\index.php [ 103 ] » Kohana_Request->execute()

    • firman June 18, 2015 at 3:03 PM - Reply

      please help me, all the scripts already equated from example

      error as follows===>>>

      Database_Exception [ 8192 ]: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

      MODPATH\database\classes\kohana\database\mysql.php [ 67 ]

      62 catch (Exception $e)
      63 {
      64 // No connection exists
      65 $this->_connection = NULL;
      66
      67 throw new Database_Exception(‘:error’,
      68 array(‘:error’ => $e->getMessage()),
      69 $e->getCode());
      70 }
      71
      72 // \xFF is a better delimiter, but the PHP driver uses underscore

      MODPATH\database\classes\kohana\database\mysql.php [ 171 ] » Kohana_Database_MySQL->connect()

      MODPATH\database\classes\kohana\database\mysql.php [ 360 ] » Kohana_Database_MySQL->query(arguments)

      MODPATH\orm\classes\kohana\orm.php [ 1504 ] » Kohana_Database_MySQL->list_columns(arguments)

      MODPATH\orm\classes\kohana\orm.php [ 392 ] » Kohana_ORM->list_columns(arguments)

      MODPATH\orm\classes\kohana\orm.php [ 337 ] » Kohana_ORM->reload_columns()

      MODPATH\orm\classes\kohana\orm.php [ 246 ] » Kohana_ORM->_initialize()

      MODPATH\orm\classes\kohana\orm.php [ 37 ] » Kohana_ORM->__construct(arguments)

      APPPATH\classes\controller\article.php [ 11 ] » Kohana_ORM::factory(arguments)

      {PHP internal call} » Controller_Article->action_index()

      SYSPATH\classes\kohana\request\client\internal.php [ 118 ] » ReflectionMethod->invoke(arguments)

      SYSPATH\classes\kohana\request\client.php [ 64 ] » Kohana_Request_Client_Internal->execute_request(arguments)

      SYSPATH\classes\kohana\request.php [ 1138 ] » Kohana_Request_Client->execute(arguments)

      DOCROOT\index.php [ 103 ] » Kohana_Request->execute()

Leave a Reply