Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Posts with comments, comments with comment types
  • Let's say I have these tables: POSTS
    id (primary key) COMMENTS (an identifying relationship)
    comment_type_id (foreign key and part of primary key)
    post_id (foreign key and other part of primary key) COMMENT_TYPES
    id (primary key)
    name (name of the comment type) Comments has no id for itself, because the combination "type of comment" and "post" is unique. (maybe bad example with posts and comments, but doesn't matter). So I have three models: post, comment and comment_type. What is the relationship between comment and comment_type? And what do I have to do to get all the info (post with its comments, comment with its comment_type)
    $post = Model_Post::find();
    $post->comments; //all comments
    $post->comment[1]->comment_type->name; //how do I get that 'name'?
    
  • ORM does not allow you to mix primary keys and foreign keys, so this is not a design option.
  • Okay no problem, I've added a primary key (id) to that table, and the foreign keys are now just foreign keys (no part of the primary key anymore). But I still don't know how to fetch the values of the comment_types table. This works, but there must be an easier way, I guess?
    $post  = Model_Post::find(1);
    foreach($post->comments as $comment) 
    {
        //this way it works:
        $comment_type = Model_Comment_Types::find($comment['comment_type_id']); //fetches the fields and values of the comment_type
        echo $comment_type->{fieldname from comment_types table}; //this works
    
        //but I'd like to use something like this, if possible (without fetching the Model_Comment_Type manually):
        $comment_type = $comment->comment_type->{fieldname from comment_types table}
    }
    

    Anyone?
  • Problem solved, one database relation was not correct. I've fixed that and now it works as expected:
    foreach($post->comments as $comment)
    {
        echo $comment->comment_type->{fieldname from comment_types table};
    }
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion