Skip to content
Advertisement

Phalcon ORM – integer field id is string after save

I’m using Phalcon for my current project and i’m seeing some strange behaviour with the type casting.

I have this model

class Customer extends Model {
    protected $id;
    protected $name;
}

In my service, i save a new Customer model like this:

$customer = new Customer();
$customer->name = 'test';
$customer->save();
var_dump($customer->id);

The problem i’m facing is that the id is a string, instead of an integer. In db the fiend of course is an auto increment int, primary key.

Also, I have already added those lines in my db connection

Pdo::ATTR_PERSISTENT => false,
Pdo::ATTR_EMULATE_PREPARES => false,
Pdo::ATTR_STRINGIFY_FETCHES => false,

which only solves the problem when you fetch from database and not when you save.

I don’t want to use a getter for id, since I have implemented another functionality with magic setters/getters.

Any solution on this issue?

Advertisement

Answer

The correct issue for this problem is https://github.com/phalcon/cphalcon/issues/13002 and has been resolved now. Just use

Model::setup([ 'castLastInsertIdToInt' => TRUE ]);

in your bootstrapping to activate workaround globally

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement