开发者

Kohana ORM Relationships

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] => [开发者_JAVA百科_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 FROMcategories` [_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.


Posts is a relationship, not a collection. Use find_all() or find() on a relationship to return a collection of records.

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

You can also apply various other methods to the relationship to filter the returned results using orderby, where, etc...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜