We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

SkipAttributes not working for Primary key (AUTO_INCREMENT)

Hi!

I was struggling with my Phalcon project because of this. I am using Annotations with my Models, and I created one to simulate the skipAttributes feature. I used it in a Model with an id attribute ( @Primary and @Identity annotations). I wanted to skip this column in order to avoid setting it manually - it is an auto incremented column in the database. But I realized that it did not work like that - it allowed me to set the value of the id and inserted the new row like that - changing the NextAutoIndex of the table.

I know my attribute should not be public and should not have a setter, but in terms of the ORM process, is there a reason behind this that I am missing?

Thanks :)

@Primary and @Identity annotations must work to skip the column, have you added a events manager to the connection to see which SQL statements are being sent to the server? https://docs.phalcon.io/en/latest/reference/db.html#logging-sql-statements

edited May '15

Hi Andres. I set the log and the result is what I expected. I'm using a Test Model, with the following class annotations:

/** 
* Test Model
* 
* @Source('test');
* @SkipOnCreate(['ignore_create','id']);
* @SkipOnUpdate(['ignore_update']);
*/
class Test extends Model
{
    /**
     * @Primary
     * @Identity
     * @Column(type="integer", nullable=false, column="id")
     */
    public $id;
    (...)

And id column of the source table has a currently low next autoindex. If I insert a new row test without setting the id, it works as expected. Inserting a new row test with a set id:

    /** Setting values **/

    $test = new Test();
    $test->id = 600;
    $test->text='abcABC123';
    $test->string='Hi ID!';
    $test->date = '2015-01-01';
    $test->datetime = '2015-01-01 00:00:00';
    $test->float = '0.01';
    $test->ignore_create = '999';

The log entry is the following: [Wed, 13 May 15 16:48:34 +0100][INFO] INSERT INTO pre_test (string, text, date, datetime, float, id) VALUES (?, ?, ?, ?, ?, ?)

It skips the ignore_create attribute but not the id attribute.

Are you using the memory meta-data adapter or other?

I'm using Files meta-data adapter

Files are only intented for being used in a production environment, are you using memory in development?

Andres, I did change to Memory Adapter, but the result was the same...

Files are only intented for being used in a production environment, are you using memory in development?