Welcome Guest, Not a member yet? Register   Sign In
Model relationships
#1

[eluser]bayowar[/eluser]
I'm looking for advice/best practices for a simple problem. I searched the forums and documentation and didn't find much.

I have one model (Comments_model) that fetches all comments to a blog entry.
Part of each comment is data from a second model (User_model), that should get merged with each of my comments using a foreign key.

Or, more generally, I'd like to fetch an array of results from Model1 and merge each row with related data from Model2.

Without models, I'd JOIN the datasets in SQL and have one loop in my view.

With proper models, the best I (and most others, acording to my search results) could come up with is this:
Code:
class Comments_model extends Model {
{
    ...

    function getComments($entry_id)
    {
        $query = boring_db_stuff();

        if($query->num_rows())
        {
            $comments = $query->result_array();

            foreach($comments AS $key => $comment)
            {
                $comment_author = $CI->User_model->getUserData($comment['user_id']);
                $comment['user'] = get_object_vars($comment_author);

                $comments[$key] = $comment;
            }

            return $comments;
        }
        else
        {
            return 0;
        }
    }
}
... which loops twice: Once in the model and once in the view.

I can't do something like while($comment = $CI->Comments_model->getComment()), either. That would put controller logic in my view.

Admittedly, this is not the worst of problems, but it applies to a lot of cases and I'd rather safe a loop in each of those.


Please tell me what I'm missing here, I can't figure it out for the life of me.


Messages In This Thread
Model relationships - by El Forum - 01-13-2009, 06:24 PM
Model relationships - by El Forum - 01-13-2009, 10:52 PM
Model relationships - by El Forum - 01-13-2009, 11:06 PM
Model relationships - by El Forum - 01-14-2009, 02:44 AM
Model relationships - by El Forum - 01-14-2009, 04:30 AM
Model relationships - by El Forum - 01-14-2009, 05:36 AM
Model relationships - by El Forum - 01-14-2009, 06:11 AM
Model relationships - by El Forum - 01-14-2009, 06:31 AM
Model relationships - by El Forum - 01-14-2009, 08:23 AM
Model relationships - by El Forum - 01-15-2009, 12:55 PM



Theme © iAndrew 2016 - Forum software by © MyBB