[eluser]ntheorist[/eluser]
@dexcell
Here's how you would set up your tables in your example:
table list
object tables
users ( id, username)
languages (id, language )
entries ( id, content )
friends ( id, mute, field2, field3, etc. )
linking tables
languages_users ( id, language_id, user_id )
friends_users (id, friend_id, user_id )
entries_users (id, entry_id, user_id )
entries_languages (id, entry_id, language_id )
Models -
the models need to relate both ways, ie if user->has_one['language'] then language must has_one['user'] or has_many['user']
user.php :
Code:
has_one = array('language');
has_many = array('entry','friend');
entry.php :
Code:
has_one = array('user','language');
friend.php :
Code:
has_one = array('user');
language.php :
Code:
has_many = array('user','entry');
it can be a pain to port foreign keys over to the new table setup. I've already had to deal with it and haven't even tried to find any software to do it for me. what i usually do is write a script in a temporary controller that does it. It just reads the table with the keys and creates INSERT statements that i can just dump into PhpMyAdmin or something, so in your example (using CI's active record from a controller) here's what you could do to parse the 'entries' table you had with the foreign keys :
Code:
// table 'entries' - (id, user_id(FK), language_id(FK), content)
$this->db->from('entries');
$query = $this->db->get();
foreach($query->result() as $row)
{
$sql = "INSERT INTO `entries_languages` SET `entry_id` = '".$row->id."', `language_id` = '".$row->language_id."' LIMIT 1;";
$sql2 = "INSERT INTO `entries_users` SET `entry_id` = '".$row->id."', `user_id` = '".$row->user_id."' LIMIT 1;";
echo $sql . br() . $sql2 . br();
}
that should spit out all the insert statements you just copy/paste and dump into the database. Just do that for every table that has keys, and write it up to insert into the linking table for each key.. so if you have 4 FKs you'll create 4 separate INSERT statements
then when that's run, just drop the user_id and language_id columns from 'entries' and you should be fine.
also, you could optionally create each $sql statement and then run $this->db->query($sql), or even run
Code:
$this->db->insert('entries_languages',array('entry_id'=>$row->id,'language_id'=>$row->language_id));
but personally i like to see the statements it creates before running them, since i want to make sure they're correct.
CC