Nilesh Wagmare Mon Aug 01 00:05:36 -0400 2011

Subject: how i can save many-to-many relationship

i am relatively new to ActiveRecord and ORM,
i am using many-to-many relationship between programs and users
i have tables programs, users and programusers which conatins program_id and user_id

class Program extends ActiveRecord\Model {
static $has_many = array(
array('users', 'through' => 'programusers')
);
}

class User extends ActiveRecord\Model {
static $has_many = array(
array('programs', 'through' => 'programusers')
);
}

if i do
$program=find('first');
$program->create_users(array('name'=>'jim','last'=>'roy' )); // i also tried build_user method also
$program->save();
error given is => Undefined property: User->program_id
but no user is created and programusers has no relationship in tables
what is wrong and what should i do to save this relationship

--
PS : i sure using php5.3


Beau Frusetta Wed Aug 03 18:15:19 -0400 2011

The relationship from Program to User assumes that the User model has the field "program_id" on it for a valid "has_many" relationship association. The opposite is also true where the "has_many" relationship on User requires that "user_id" exist on Program.

The easiest way to get around this would be to create a table in between them to store pointers to each relationship that has "program_id" and "user_id" in it. Then tie those tables together using that table.

(1-1/1)