Welcome Guest, Not a member yet? Register   Sign In
Need DB, Model, IgnitedRecord guidance
#2

[eluser]m4rw3r[/eluser]
Well, IgnitedRecord can automate a lot, but not everything, so I'll make an example for you:

(and I do recommend that you try the tutorials found here too)

Code:
class posts{
function posts(){
    // set up models
    $this->user = IgnitedRecord::factory('users');
    $this->user->habtm('groups'); // Has And belongs To Many
    $this->user->has_many('posts');
    $this->group = Ignitedrecord::factory('groups');
    $this->group->habtm('users');
    $this->post = IgnitedRecord::factory('posts');
    $this->post->belongs_to('user');
}

// just fetch the posts
function index()
{
    $posts = $this->post->order_by('title', 'desc')->limit(10)->find_all();

    // display them
}

// fetch the posts made in a group:
function by_group($id)
{
    $group = $this->group->find($id);

    // this query is complicated (and IR cannot really help so much, something I'll implement)
    $sub = new Query();
    $sub->select('user_id')
        ->from('groups_users')
        ->where('group_id', $group->id);

    // fetch the posts, with a subquery to filer by users
    $posts = $this->post->where_in('user_id', $sub)
                        ->order_by('title', 'desc')
                        ->limit(10)
                        ->find_all();

    // make a view
}

// fetch the posts made by a user:
function by_user($id)
{
    $user = $this->user->find($id);
    $posts = $user->related('posts')
                  ->order_by('title', 'desc')
                  ->get();

    // render view
}

function save()
{
    // I'll just cover the saving, normally you would add some
    // validation that displays a form if it fails

    $post = $this->post->new_record();
    $post->title = $this->input->post('title');
    $post->body = $this->input->post('body');

    // $this->current_user is the current user id

    $user = $this->user->find($this->current_user);

    // now save the post and relate it to the user
    // The records are automatically saved when creating relations
    
    $post->add('user', $user);
}
}

I also recommend to check the included manual, it covers a lot.

The cascading deletes are something you have to do in the table constraints, not in IgnitedRecord (I'll maybe add it in a later version).


Messages In This Thread
Need DB, Model, IgnitedRecord guidance - by El Forum - 08-29-2008, 05:57 PM
Need DB, Model, IgnitedRecord guidance - by El Forum - 08-31-2008, 09:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB