Kohana ORM 关系
我正在尝试掌握 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] => => 数组 ( ) [_callbacks:protected] => 数组 ( ) [_filters:protected] => 数组 ( ) [_labels:protected] => 数组 ( ) [_object:protected ] id] => 1 [标题] => ) [_changed:protected] => 数组 () [_related:protected] => 数组 () [_loaded:protected] => _saved:protected] => 1 [_sorting:protected] => 数组 ( [id] => ASC ) [_foreign_key_suffix:protected] => _id [_object_name:protected] => 类别 [_object_plural:protected] > 类别 [_table_name:protected] => 类别 [_table_columns:protected] => 数组 ( [id] => 数组 ( [type] => int [min] => 0 [最大] => 65535 [列名] => id [列默认值] => [数据类型] => Smallint 无符号 [is_nullable] => [序数位置] => 1 [显示] => 6 [评论] => [额外] => auto_increment [键] => PRI [特权] =>选择、插入、更新、引用 ) [标题] => Array ( [type] => string [character_maximum_length] => 65535 [column_name] => title [column_default] => [data_type] => text [is_nullable] => [ordinal_position] => 2 [ collation_name] => latin1_swedish_ci [注释] => [键] => [权限] => 选择、插入、更新、引用) Array ( [type] => string [character_maximum_length] => 65535 [column_name] => 描述 [column_default] => [data_type] => text [is_nullable] => [ordinal_position] => 3 [ collation_name] => latin1_swedish_ci [额外] => [权限] => 选择、插入、更新、引用 ) ) [_ignored_columns:protected] =>数组 ( ) [_updated_column:protected] => [_created_column:protected] => [_primary_key:protected] => id [_primary_val:protected] =>名称 [_table_names_plural:protected] => 1 [_reload_on_wakeup:protected] => 1 [_db:受保护] => Database_MySQL 对象 ( [_connection_id:protected] => d6ad44aa068ae69071b9614f2a4a760bf55d9307 [_identifier:protected] => [last_query] => 显示类别中的完整列` [_instance:protected] => 默认 [_connection :protected] => 资源 id #36 [_config:protected] => 数组 ( [type] => mysql [连接] => 数组 ( [主机名] => localhost [数据库] => cms_test_kohana [持久] => ) [表前缀] => [字符集] => [缓存] => [分析] => 1) ) [_db_applied:protected]数组 ( ) [_db_pending:protected] =>数组 ( ) [_db_reset:protected] => 1 [_db_builder:受保护] => [_with_applied:受保护] =>数组 ( ) [_preload_data:protected] =>数组 ( ) )
有人可以帮忙吗?
任何建议表示赞赏。
谢谢。
I'm trying to get to grips with the Kohana ORM but I'm a bit stuck.
I have created category and post models, with a relationship based on category_id.
I can fetch post and category models separately, however I cannot seem to fetch them using the relationship. For example:
// Get first category
$category = ORM::factory('category', 1);
foreach ($category->posts as $post)
{
echo $post->title;
}
The above just gives me a blank screen (I know I shouldn't be outputting anything in the controller, just trying to get it working first).
print_r($category);
Gives me this:
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 ( ) [_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] => Array ( [type] => int [min] => 0 [max] => 65535 [column_name] => id [column_default] => [data_type] => smallint unsigned [is_nullable] => [ordinal_position] => 1 [display] => 6 [comment] => [extra] => auto_increment [key] => PRI [privileges] => select,insert,update,references ) [title] => Array ( [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] => Array ( [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 ) ) [_ignored_columns:protected] => Array ( ) [_updated_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 Object ( [_connection_id:protected] => d6ad44aa068ae69071b9614f2a4a760bf55d9307 [_identifier:protected] => [last_query] => SHOW FULL COLUMNS FROM
categories` [_instance:protected] => default [_connection:protected] => Resource id #36 [_config:protected] => Array ( [type] => mysql [connection] => Array ( [hostname] => localhost [database] => cms_test_kohana [persistent] => ) [table_prefix] => [charset] => utf8 [caching] => [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 ( ) )
Can anyone help?
Any advice appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
帖子是一种关系,而不是集合。对关系使用 find_all() 或 find() 可返回记录集合。
您还可以将各种其他方法应用于关系,以使用 orderby、where 等过滤返回的结果...
Posts is a relationship, not a collection. Use find_all() or find() on a relationship to return a collection of records.
You can also apply various other methods to the relationship to filter the returned results using orderby, where, etc...