Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Getting a primary key error trying to create a Model
  • I'm trying to map a response object from a fb graph request directly into an ORM model but keep getting the error that the primary key cannot be changed.

    [code]
    class Model_Facebook extends \Orm\Model {


    protected static $_properties = array(
    'id', //id from fb
    'user_id', //my user id
    'name',
    'gender',
    'birthday',
    'email',
    'link',
    'username',
    'verified',
    'created_at',
    'updated_at'
    );
    protected static $_observers = array(
    'Orm\Observer_CreatedAt',
    'Orm\Observer_UpdatedAt',
    );
    protected static $_belongs_to = array(
    'user' => array(
    'key_from' => 'user_id',
    'model_to' => 'Model_Profile',
    'key_to' => 'user_id',
    'cascade_save' => false,
    'cascade_delete' => false,
    ));

    }
    [/code]

    Is how the model looks but the id is not an auto generated PK like most models have it's the id I'm getting from facebook so I think that has something to do with it. Whenever I go to save the model with the id specified I get

    [code]Fuel\Core\FuelException [ Error ]: Primary key cannot be changed.[/code]

    [code]
    //add our id to the arr and map the fb id to our facebook id column
    $profile['user_id'] = $this->user_id;

    $model_facebook = \Model_Facebook::forge($profile);

    // var_dump($model_facebook);
    //
    $model_facebook->save();
    [/code]

    where $profile is the array of data I'm trying to map to my db. I get that the ORM models default to use id as the primary key field but regardless I'm not changing it anywhere.
  • Which version of Fuel are you running?

    What is the result of var_dump($model_facebook->is_new()); before the save()?
  • Fuel 1.3

        var_dump is as follows. is object(Model_Facebook)#35 (9)  trying to force the id as 35 possibly? I know I can work around this by just adding an explicit facebook_id column to the table and use id normally but want to figure this out.

        object(Model_Facebook)#35 (9) {
      ["_is_new":protected]=>
    bool(true)
    ["_frozen":protected]=>
    bool(false)
    ["_data":protected]=>
    array(9) {
    ["id"]=>
    string(7) ""
    ["user_id"]=>
    string(1) "1"
    ["name"]=>
    string(11) ""
    ["gender"]=>
    string(4) "male"
    ["birthday"]=>
    string(10) ""
    ["email"]=>
    string(18) ""
    ["link"]=>
    string(34) ""
    ["username"]=>
    string(10) ""
    ["verified"]=>
    bool(true)
    }
    ["_original":protected]=>
    array(0) {
    }
    ["_data_relations":protected]=>
    array(0) {
    }
    ["_original_relations":protected]=>
    array(0) {
    }
    ["_reset_relations":protected]=>
    array(0) {
    }
    ["_view":protected]=>
    NULL
    ["_iterable":protected]=>
    array(0) {
    }
    }

    I edited out actual profile data
  • It says
        ["id"]=> string(7) ""
    So it sees a string 7 long, yet it doesn't display anything?
  • No there a string integer there I just removed it to post here. Does it matter if id isn't an actual PK on the database?
  • What do you mean "actual PK"?

    Every model has to define a PK, it doesn't have to be an auto-increment field. It's perfectly fine to provide the key yourself.

    What does is_new() return before you save()?
  • is_new() dumps

          bool(true)
  • Ok, that's good. So the Model is in a new state, and should INSERT the data without problems.

    Are you sure the error is generated by this model, and not by a related model? The most common cause for this issue is that you have defined a PK as an FK somewhere, which is not allowed.
  • Well user_id is an indexed key in Model_Profile. Heres another odd thing. The error is being thrown when I try to save the model but if I look in the database I can see the record created. Here are the models

    class Model_Profile extends \Orm\Model {

        protected static $_has_one = array(
            
            'twitter' => array(
                'key_from' => 'user_id',
                'model_to' => 'Model_Twitter',
                'key_to' => 'user_id',
                'cascade_save' => false,
                'cascade_delete' => false,
            ),
            'facebook' => array(
                'key_from' => 'user_id',
                'model_to' => 'Model_Facebook',
                'key_to' => 'user_id',
                'cascade_save' => false,
                'cascade_delete' => false,
            )
             
        );
        protected static $_belongs_to = array(
            'user' => array(
                'key_from' => 'user_id',
                'model_to' => 'Model_User',
                'key_to' => 'id',
                'cascade_save' => false,
                'cascade_delete' => false,
            )
        );
    }

    and 

    class Model_Facebook extends \Orm\Model {

        protected static $_properties = array(
            'id',
            'user_id',
            'name',
            'gender',
            'birthday',
            'email',
            'link',
            'username',
            'verified',
            'created_at',
            'updated_at'
        );
        protected static $_observers = array(
            'Orm\Observer_CreatedAt',
            'Orm\Observer_UpdatedAt',
        );
        protected static $_has_one = array(
            'user' => array(
                'key_from' => 'user_id',
                'model_to' => 'Model_Profile',
                'key_to' => 'user_id',
                'cascade_save' => false,
                'cascade_delete' => false,
                ));

    }
  • Because the error isn't thrown on the save of the record, but on the processing of the related objects.

    Your Model_Profile has a belongs_to to Model_User, which doesn't seem correct. If the FK of that relation is in Model_Profile, it should be a has_one, and the other side a belongs_to.

    This is the likely cause of the error, because by defining it this way, the 'id' field in Model_User is seen as the FK, and it the PK in Model_User.
  • I removed the relationship from Model_Facebook and still get a PK change error.
  • It must be there somewhere, as it is an error you only get if a FK in a relation definition is a PK in the model it points to.

    Remove all relations, and see if the issue is gone. If so, add the relations one at the time until the problem appears again. Note that is doesn't have to be caused by a Model_Facebook relation, it can be an issue further down the line...
  • You can try to replace the error message with

    throw new \FuelException('Primary key on model '.get_class().' cannot be changed.');

    so you can see which on which model the key is reset.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion