cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

skipAttributes/skipAttributesOnCreate does not work for Primary keys

Open nsossonko opened this issue 11 years ago • 7 comments

Hi,

I am using Mysql and cannot get my primary keys to be skipped during create. In Mysql my primary keys are set to AUTO_INCREMENT. I call model->skipAttributes('id') in initialize, to no avail. If a primary key is set on the model, it will be passed to Mysql and set to that value.

class Samples extends \Phalcon\Mvc\Model {
    /**
     * @Primary
     * @Identity
     * @Column(type="integer", nullable=false, length=16)
     */
    public $id;

    public function initialize() {
        // set model to use dynamic updates
        $this->useDynamicUpdate(true);

        // id columns will always be auto-generated and immutable
        $this->skipAttributes(array('id'));
    }
}

$sample = new Samples();
$sample->id = 5;
$sample->create();
$record = Samples::findFirst(5);
var_dump($record->toArray());
CREATE TABLE `samples` (
    `id` bigint(16) unsigned NOT NULL AUTO_INCREMENT
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

nsossonko avatar Sep 11 '15 14:09 nsossonko

What version of Phalcon are you using?

andresgutierrez avatar Sep 11 '15 14:09 andresgutierrez

2.0.7

nsossonko avatar Sep 11 '15 14:09 nsossonko

Fields marked with the @Identity are automatically assigned by Phalcon, remove this annotation will do the work for you

andresgutierrez avatar Sep 11 '15 14:09 andresgutierrez

I removed it and still same story. The id is whatever is assigned to it, it is not skipped.

nsossonko avatar Sep 11 '15 14:09 nsossonko

Clear your metadata cache and try again. May helps...

On Fri, 11 Sep 2015 19:05 nsossonko [email protected] wrote:

I removed it and still same story. The id is whatever is assigned to it, it is not skipped.

— Reply to this email directly or view it on GitHub https://github.com/phalcon/cphalcon/issues/10932#issuecomment-139562457.

sszdh avatar Sep 12 '15 08:09 sszdh

Use $this->skipAttributesOnCreate(array('id')); for skipping them, when you want to create a new record. Works fine for me.

But why do you set an id if you want that this is skipped?

Fuhunter avatar Sep 13 '15 21:09 Fuhunter

I switched to using getters/setters for what I was doing as it made more sense anyhow. If this is tested and works, you can close this issue.

nsossonko avatar Sep 17 '15 17:09 nsossonko