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.

有帮助吗?

解决方案

  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.

其他提示

At the firm I work for we were looking into upgrading to 3.2 very recently. However, in our evaluation I don't recall seeing a difference in ORM handling methods. Yours above looks like it should be something like this:

  $Blog_Post = ORM::factory('blogpost')->where('id', '=', 1)->find();

  $content = $Blog_Post->content;

Assuming your table is called blogposts, of course. I may be wrong about that and if I am, can you link to the documentation that shows this kind of model interaction?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top