我正试图掌握Kohana ORM,但我有点陷入困境。 我创建了类别和帖子模型,其关系基于category_id。
我可以单独获取帖子和类别模型,但我似乎无法使用关系获取它们。例如:
// Get first category
$category = ORM::factory('category', 1);
foreach ($category->posts as $post)
{
echo $post->title;
}
以上只是给我一个空白屏幕(我知道我不应该在控制器中输出任何东西,只是试图让它先工作)。
print_r($category);
给我这个:
Model_Category Object([_has_many:protected] => Array([posts] => Array([model] => post [foreign_key] => category_id [through] => [far_key] => post_id))[_ has_one:protected] => Array()[_ belongs_to:protected] => Array()[_ load_with:protected] => Array()[_ validate:protected] => [_rules:protected] = > Array()[_ callbacks:protected] => Array()[_filters:protected] => Array()[_ label:protected] => Array()[_ object:protected] =>数组([id ] => 1 [title] => [description] =>)[_ changed:protected] => Array()[_related:protected] => Array()[_ loaded:protected] => [_saved :protected] => 1 [_sorting:protected] =>数组([id] => ASC)[_ foreign_key_suffix:protected] => _id [_object_name:protected] =>类别[_object_plural:protected] =&gt ; categories [_table_name:protected] => categories [_table_columns:protected] => Array([id] => Array([type] => int [min] => 0 [max] =&g吨; 65535 [column_name] => id [column_default] => [data_type] => smallint unsigned [is_nullable] => [ordinal_position] => 1 [显示] => 6 [评论] => [extra] => auto_increment [key] => PRI [privileges] =>选择,插入,更新,引用)[title] =>数组([type] => string [character_maximum_length] => 65535 [column_name] => title [column_default] => [data_type] => text [is_nullable] => [ordinal_position] => 2 [ collation_name] => latin1_swedish_ci [comment] => [extra] => [key] => [privileges] => select,insert,update,references)[description] =>数组([type] => string [character_maximum_length] => 65535 [column_name] => description [column_default] => [data_type] => text [is_nullable] => [ordinal_position] => 3 [ collation_name] => latin1_swedish_ci [comment] => [extra] => [key] => [privileges] => select,insert,update,references))[_ signored_columns:protected] => Array()[_ update_column:protected] => [_created_column:protected] => [_primary_key:protected] => id [_primary_val:protected] => name [_table_names_plural:protected] => 1 [_reload_on_wakeup:protected] => 1 [_db:protected] => Database_MySQL对象([_connection_id:protected] => d6ad44aa068ae69071b9614f2a4a760bf55d9307 [_identifier:protected] => [last_query] => SHOW FULL COLUMNS FROM
categories` [_instance:protected] =>默认[_connection:protected] =>资源ID#36 [_config :protected] =>数组([type] => mysql [连接] =>数组([hostname] => localhost [数据库] => cms_test_kohana [persistent] =>)[table_prefix] => [charset] => utf8 [缓存] => [profiling] => 1))[_ db_applied:protected] => Array()[_ db_pending:protected] => Array()[_ db_reset:protected] => 1 [_db_builder:protected] => [_with_applied:protected] => Array()[_ preload_data:protected] => Array())
有人可以帮忙吗?
任何建议表示赞赏。
感谢。
答案 0 :(得分:2)
帖子是一种关系,而不是一个集合。在关系上使用find_all()或find()可返回记录集合。
foreach ($category->posts->find_all() as $post) {
echo $post->title;
}
您还可以对关系应用各种其他方法,以使用orderby,where等来过滤返回的结果...