• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need DB, Model, IgnitedRecord guidance

#1
[eluser]Unknown[/eluser]
I have scanned some DB threads and am very excited to use IgnitedRecord.

I have never used rails and in fact am most used to MS SQL and using views or stored procedures to do model stuff - new to MVC also. So, given that I know DB/SQL well but am not at all familiar with this style of programming - where would you suggest I start? (Just to make things hard, am also having to refresh on PHP...heheh...but syntax seems ok so far)

I am trying to build a very simple application but am not sure how to approach the relational nature of the DB for CRUD.

If I understand correctly (so far), you create Models to automate a lot of joining and special SQL queries yes? And then something like IgnitedRecord could automate things further to automate the enforcing of relationships like cascading deletes and updates?

Any input, reference to tutorial, video, proper manual chapter, etc would be helpful. A very simple tutorial using a relational data would be ideal!

Thanks in advance. Smile

#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).


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.