Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
[ Error ]: Primary key cannot be changed.
  • I am having a problem trying to save a new model when the value for the primary key is set. I am using v1.3


    class Model_UserPasswordResetRequest extends Orm\Model {

    protected static $_table_name = 'user_pword_reset_requests';
    protected static $_primary_key = array('hash');

    protected static $_properties = array('hash', 'user_id', 'used', 'ipaddress', 'createdon');

    public function _event_before_insert()
    {
    $this->createdon = \DB::expr('NOW()');
    $this->ipaddress = Input::ip();
    }

    protected static $_observers = array('Orm\\Observer_Self' => array('events' => array('before_insert')));

    }

    Then in my controller I have -


    $req = Model_UserPasswordResetRequest::forge(array(
    'hash' => hash('sha256', $user->name . $user->email . $user->mobile . microtime()),
    'user_id' => $user->id
    ));
    $req->save();

    This throws a FuelException whenever it is executed. The values are stored in the table as expected but the exception is thrown. I am probably missing something simple but right now I am unable to see what it is. Any help will be very much appreciated.

    TIA

    Nick

    P.S. Is there a way to post code so that it is formatted in a more readable fashion?
  • To start with the ps: for the moment you'll have to use the "show source" button and manually enclose the snippit with PRE tags. I've been trying to convince the forum builders to add such a button, but so far no luck...

    Where exactly is this exception thrown? Does this model have related models that could cause this?

    On an ORM model, you can set a key value, only when the object state is NEW, and the key hasn't been set. Once it's set, you can no longer alter it. That includes observers. This code, passing the key to forge(), should not be a problem at all.
  • The exception is thrown in "PKGPATH/orm/classes/model.php @ line 941". The model is exactly as shown above. It has no relations defined in the code. The controller has no other code referencing this model. I have tried dumping $req->is_new() before the save and it returns true.
  • You're still on v1.3?

    Can you post the entire backtrace?
  • Hi Harro,

    Thanks for your assistance with this. Yes, I am still using v1.3. The backtrace is -

    PKGPATH/orm/classes/model.php @ line 817
    PKGPATH/orm/classes/model.php @ line 1086
    PKGPATH/orm/classes/model.php @ line 1024
    APPPATH/classes/controller/user.php @ line 167
    COREPATH/classes/controller/rest.php @ line 129
    COREPATH/classes/request.php @ line 420
    DOCROOT/index.php @ line 38
  • HarroHarro
    Accepted Answer
    You might want to consider upgrading (to 1.4, or 1.5 which is around the corner).

    This bug has been fixed over 4 months ago: https://github.com/fuel/orm/commit/cf4c397ec71ba6e5fcd05b8b91328dd46eed734d
  • Thanks for pointing me at that commit. I have implemented that fix as a temporary solution until I can spend the time upgrading to v1.4.

Howdy, Stranger!

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

In this Discussion