Tuesday, 27 August 2013

FuelPHP Get "Not null violation" on foreign key trying to insert into related model

FuelPHP Get "Not null violation" on foreign key trying to insert into
related model

I'm trying to insert into the table of a model with multiple levels of
HasMany relationships. Here's the breakdown so far
Customer->(HasMany)->Members->(HasMany)->Incomes
However, on when trying to insert into the Incomes table, I get a "Not
null violation" with the foreign key from the Members table not being
carried to Incomes. I know the most common problem is screwing up the
$_has_many and $_belongs_to properties, but as far as I can tell they are
fine. Plus, Just inserting into the Member table works fine so I know at
least for the first layer it's working! The only thing I can think of is
if since it's a second level down, it's screwing up because of that.
Here's my code:
Relation Link (Member)
protected static $_has_many = array(
'incomes' => array(
'key_from' => 'id',
'model_to' => 'Model_Income',
'key_to' => 'member_id',
'cascade_save' => true,
'cascade_delete' => true,
),
);
Relation Link (Income)
protected static $_belongs_to = array(
'member' => array(
'key_from' => 'member_id',
'model_to' => 'Model_Member',
'key_to' => 'id',
'cascade_save' => true,
'cascade_delete' => true,
),
);
The Controller Code
// code to set up $customer
$customer->members[] = Model_Member::forge();
// set $member_vals here
$customer->members[0]->set($member_vals);
$customer->members[0]->incomes[] = Model_Income::forge();
// set $income_vals here
$customer->members[0]->incomes[0]->set($income_vals);
$customer->save();

No comments:

Post a Comment