Search code examples
kohana-3kohana-orm

Kohana 3.2 ORM Does not contain model info


I'm working with Kohana 3.2 and have the following code in my controller.

  $Blog_Post = new Model_Blogpost();

  $Blog_Post->where('id', '=', 1);
  $Blog_Post->find();

  $content = $Blog_Post->content;

I Currently have 3 records in my db with id's 1, 2, and 3.

$Blog_Post->content, or any other field return null. and I'm not sure why.


Solution

    1. Use ORM::factory('blogpost', $id) or new Model_Blogpost($id) if you need an object with PK == $id.

    2. Check your model after loading.

      if $Blog_Post->loaded()
      {
           // it works!
      }
      else
      {
           // record not found
      }
    

    If record not found, you can see last DB query with $Blog_Post->last_query()

    UPD. From comments. Your model will not work with this modifications. Note that ORM data stored in $_object property, and $Blog_Post->content is just a shortcut for $Blog_Post->_object['content'] via __get() method. Of course, if you define public $content property, $Blog_Post->content will return NULL value instead of using DB data.

    There is no reason for defining model fields as properties. If you need IDE hints, just use PHPDOC.