Welcome Guest, Not a member yet? Register   Sign In
Learning Datamapper ... Help required
#1

[eluser]Bainzy[/eluser]
Hey everyone,

Right I am trying to learn datamapper and I am slowly getting there but i do have a few questions.

Basically i am trying to port Dove Forums over to datamapper for the new version as I think it will be a huge improvement on performance. However im getting confused when it comes down to saving relationships. Here is what I have worked out :

Parent Forums - Can have many Sub Forums.
Sub Forums - Can only have one parent.
Topics - Can have many users and posts, but only have one parent forum.

anyway i moved on to coding the home screen. Now here i need the parent forums and then the sub forums for each parent ... here is the code i have so far :

Code:
// Get required ojects.
        $topic = new Topic();
        $post = new Post();
        $p = new parent_forum();
        $s = new sub_forum();
                
        // First let`s get the parent forums.
        $p->where('active', 'yes')->get();
        
        foreach($p as $parent)
        {
            // Now let`s get all the sub forums.
            $s->where(array('parent_id' => $parent->id, 'active' => 'yes'))->get();
            
            // foreach sub forum save the relationship.
            foreach($s as $sub)
            {
                $sub->save($parent);
            }
            
            // build the parent forums data.
            $data['parent_forums'][] = array(
                'id' => $parent->id,
                'name' => $parent->name,
                'description' => $parent->description,
                'active' => $parent->active,
            );
            
            foreach($s->parent_forum->get()->sub_forum->get() as $forums)
            {
                // Build the sub forums data.
                $data['forums_' . $parent->id][] = array(
                    'forum_name' => anchor(''.site_url().'/topics/view_topics/'.$forums->id.'', ''.$forums->name.''),
                    'forum_description' => $forums->description,
                    'topic_count' => $topic->where(array('parent_forum_id' => $forums->id, 'active' => 'yes'))->count(),
                    'post_count' => $post->where(array('forum_id' => $forums->id, 'active' => 'yes'))->count(),
                );
            }

Now the code above creates the relationship between parent and sub forums perfectly but do I have to call the following code every execution ? :

Code:
// foreach sub forum save the relationship.
            foreach($s as $sub)
            {
                $sub->save($parent);
            }

As obviously as a admin creates new forums in the admin panel and new sub forums the relationship on the home screen may change and would need to be updated. Would i handle this in the admin panel rather than every time a user visits the page ?

I hope you understand what I mean.

Kind Regards
Chris




Theme © iAndrew 2016 - Forum software by © MyBB