views:

472

answers:

3

Can anyone point out a good many-to-many database tutorial for CodeIgniter.

Just trying to work out the process of creating, and then updating a many-to-many relationship. My example uses a multi-select of values, wondering how you take care of monitoring changes on update etc.

+1  A: 

I'd like to share what I do in my application. This is basically same with my answer in this question.

  1. After user submit, and before entering to database, I will fetch the existing data in the database into an array. Example: $collection = array('111', '112', '113', '114'); (This is just for example. In real, it should fetch from database then put the value to array)
  2. I will check the new user input in two step. First step is to see if it already in the database or not. If it not, then insert. Otherwise ignore:
    foreach ( $inputs as $input )
    {
      if ( ! in_array($input, $collection) )
      {
        //do insert here
      }
    }

Then in second loop, I do it in reverse, to delete the data that not selected by user.

foreach ( $collection as $data )
{
  if ( ! in_array($data, $inputs) )
  {
    //do delete here
  }
}

In your case, you might or might not need the second loop. I needed this since I make the input as checkboxes, that the user can choose to activate / deactivate, thus I translate it as insert and delete.

Since you will implement it using multi-select, then basically it's same with my checkboxes.

If you have structure or code example, feel free to share it, and I will help you fine tune it (of course with my style, that might or might not optimized yet).

Donny Kurnia
thanks. awesome. i was thinking it might have to be something like this, but was shying away from it. in the end i have gone for a much simpler, delete all associations, reinsert associations. however once i finish the app, i will go back and factor this back in.
esryl
A: 

This website has some tutorials for many to many. It uses doctrine though.

http://www.phpandstuff.com/articles/category/tutorials

shin
doctrine is next on my list, but right now i am too new to codeigniter and databases in general to take it to that next level. i.e. want to get my mind round the basics first.
esryl
A: 

The codeigniter from scratch series will cover almost anything you want to know about the framework

http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-1/

hope that helps. there are 7 to date btw

Tom Schlick
cheers for this, will take a look. i am hoping they cover many-to-many.
esryl