2011-01-10 101 views
2

我试图去处理Kohana ORM,但我有点卡住了。 我已创建类别和发布模型,并根据category_id建立关系。Kohana ORM关系

我可以分别获取帖子和类别模型,但我似乎无法使用关系获取它们。例如:

// Get first category 
$category = ORM::factory('category', 1); 

foreach ($category->posts as $post) 
{ 
    echo $post->title;     
} 

以上只是给了我一个空白屏幕(我知道我不应该在输出控制器任何东西,只是想获得它的工作第一)。

print_r($category); 

给我此:

Model_Category对象([_has_many:保护] =>数组([帖] =>数组([模型] =>讯息[foreign_key] => CATEGORY_ID [通过] =数组()[__保存] =>数组()[_validate:保护] => [_rules:protected ] ArrayList()[_callbacks:protected] => Array()[_filters:protected] => Array()[_labels:protected] => Array()[_object:protected] => Array([id] => 1 [title] => [description] =>)[_changed:protected] => Array()[_related:protected] => Array()[_loaded:protected] => [_saved:protected] => 1 [_sorting: protected] => Array([id] => ASC)[_foreign_key_suffix:protected] => _id [_object_name:protected] => category [_object_plural:protected] => categories [_table_name:protected] => categories [_table_columns:protected] => Array([id] =>数组([type] => int [min] => 0 [max] => 65535 [column_name] => id [column_default] => [data_type] => smallint unsigned [is_nullable] => [ordinal_position] =>显示] => 6 [comment] => [extra] => auto_increment [key] => PRI [privileges] =>选择,插入,更新,引用)[title] => Array([type] => string [character_maximum_length ] => 65535 [column_name] => title [column_default] => [data_type] => text [is_nullable] => [ordinal_position] => 2 [collat​​ion_name] => latin1_swedish_ci [comment] => [extra] => [key ] => [privileges] => select,insert,update,references)[description] => Array [character] > text [is_nullable] => [ordinal_position] => 3 [collat​​ion_name] => latin1 _swedish_ci [comment] => [extra] => [key] => [privileges] =>选择,插入,更新,引用))[_ignored_columns:protected] => Array()[_updated_column:protected] => [_created_column: Database_MySQL Object([_connection_id:protected] => [_primary_key:protected] => id [_primary_val:protected] => name [_table_names_plural:protected] => 1 [_reload_on_wakeup:protected] => ] => d6ad44aa068ae69071b9614f2a4a760bf55d9307 [_identifier:protected] =>[last_query] => SHOW FULL COLUMNS FROM categories [[_instance:protected] => default [_connection:protected] =>资源ID#36 [_config:protected] => Array([type] => mysql [connection ] =>数组([hostname] => localhost [database] => cms_test_kohana [persistent] =>)[table_prefix] => [charset] => utf8 [caching] => [profiling] => 1))[_db_applied: Array()[_db_pending:protected] => Array()[_db_reset:protected] => 1 [_db_builder:protected] => [_with_applied:protected] => Array()[_preload_data:protected] => Array())

任何人都可以帮忙吗?

任何意见赞赏。

谢谢。

+0

显示您的机型配置 – biakaveron 2011-01-10 16:30:23

回答

2

职位是一种关系,而不是一个集合。在关系上使用find_all()或find()来返回一组记录。

foreach ($category->posts->find_all() as $post) { 
    echo $post->title;     
} 

你也可以运用其他各种方法之间的关系使用排序依据,在那里,等筛选返回结果...