2013-08-22 47 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.