Model id setting problem [Phalcon v.1.2.1]

Is there anyway to force-set the id in a new record?

when i do:


$model = new anyModelClass;
$model->id = $di->getShared('uniqueness')->generateUid();

$model->save();

I got the register in theDatabase but not the recordTimestamp for create (it fires in the beforeCreate).

My question is : is there a problem with setting id manually?

Thanks!



15.5k

The ID is just added to the model and set when saved, so if the column configuration allows that field and the database accepts it, you can set it like you did. Do you receive some kind of error when using the solution in your example?

No error, but it doesnot fires the beforeCreate Event that i really need it.

I have now a middle solution; not for this problem but for the application to work properly.



15.5k

Do a beforeSave() instead:


class anyModelClass {

    public function beforeSave() {
        if(!isset($this->id)) {
             $this->id = "Your awesome ID generation here.";
        }
    }

}

yeah i tried it, the problem i had is the ID is not NULLABLE and when trying to save the ORM returning this message. I thing a validation stuff...

About making id NULLABLE, its not possible because its PrimaryKey.



15.5k

Hmm, that shouldn't be a problem, since it hasn't been saved. Using my code, an ID would have been created when connecting to the DB and nullification problem should not occur. It sounds like the problem is elsewhere to me. Try this code:


$obj = new myModel();
if(!$obj->save()) {
    foreach($obj->getMessages() as $m) {
        echo $m . "<br />\n";
    }
}

What are the exact error messages you receive?

yeah you're right i was sure i tried that way

anyway, dou you know when this ocurrs?

Array ( [0] => Phalcon\Mvc\Model\Message Object ( [_type:protected] => PresenceOf [_message:protected] => id is required [_field:protected] => id [_model:protected] => ) )