2013-08-22 46 views
1

我一直在用這個ORM錯誤敲打我的頭: Fuel \ Core \ FuelException [錯誤]:模型Model_CustomValue上的主鍵無法更改。FuelPHP ORM模型上的主鍵無法更改

下面是我的模型我在遇到問題的相關信息:

<?php 
use Orm\Model; 

class Model_Purchase extends Model 
{ 
    protected static $_has_many = array(
     'customvalues' => array(
      'model_to' => 'Model_CustomValue', 
      'key_to' => 'purchase_id', 
      'cascade_delete' => true, 
     ) 
    ); 

    protected static $_properties = array(
     'id', 
     'customer_id', 
     'payment_id', 
     'audit_id', 
     'created_at', 
     'updated_at', 
    ); 


<?php 
use Orm\Model; 

class Model_CustomValue extends Model 
{ 
    protected static $_table_name = 'customvalues'; 
    protected static $_primary_key = array('purchase_id', 'customfield_id'); 

    protected static $_belongs_to = array(
     'purchase' => array(
      'key_from' => 'purchase_id', 
      'model_to' => 'Model_Purchase', 
      'key_to' => 'id', 
     ), 
    ); 

當試圖與Model_CustomValue對象的數組保存Model_Purchase爲一個名爲$購買對象的「customvalues」屬性,我得到「模型Model_CustomValue上的主鍵不能更改」。

我試着在Model_CustomValue上的「belongs_to」中交換key_from /,但無濟於事。

我使用的燃油1.6(哈希:6e6d764)

請讓我知道更多的信息將是有益的,我來提供。

回答

2

FuelPHP forum thread,哈羅回答:

You can not have a column which is at the same time FK and PK. Which you have on your Model_CustomValue.

The reason for that is that when you disconnect a relation, the FK will be set to NULL, which should not happen with a PK.

我再澄清,對於我們這些誰可能需要從最初的例子具體的例子,我確認:

So just re-stating why that's not allowed:

Model_CustomValue uses the "purchase_id" as part of its PK as well as the FK to Model_Purchase. And if the two Models were to be unlinked, that would lead to a null portion of the PK for Model_CustomValue -- which obviously isn't allowed.