2012-05-22 36 views
1

我有2個表:的Kohana 3.2 HAS_ONE模型

Profiles 
------ 
id 

Stats 
------ 
profile_id 
v1 
v2 

和2款:

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

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

我做的:

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

我期望在統計與排profile_id = $ id更新或創建。相反,它總是試圖插入記錄,即使它存在(因爲它認爲沒有加載記錄)。

我該如何解決?

回答

1

您應該遵循Kohana的方式,並添加idStats表或在Model_Profile定義中指定的外鍵:

$_has_one = array('stat' => array('foreign_key' => 'profile_id')); 
+0

謝謝!儘管將Stats中的PK重命名爲id,但我必須在兩種模型中都使用'id'外鍵。 –