سؤال

I've got 2 tables:

Profiles
------
id

Stats
------
profile_id
v1
v2

and 2 models:

Profile_Model
$_has_one = array( 'stat' => array() );

Stat_Model
$_belongs_to = array( 'profile' => array() );

I do:

$profile = ORM::Factory( 'profile', $id );
$profile->stat->v1 = 123;
$profile->stat->save();

I expect row at Stats with profile_id = $id to updated or created. Instead it's always trying to INSERT record, even if it exists (as it thinks that record isn't loaded).

How do I fix it?

هل كانت مفيدة؟

المحلول

You should either follow Kohana way and add id to the Stats table or specify the foreign key in your Model_Profile definition:

$_has_one = array('stat' => array('foreign_key' => 'profile_id'));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top