skipAttributes/skipAttributesOnCreate does not work for Primary keys
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;
What version of Phalcon are you using?
2.0.7
Fields marked with the @Identity are automatically assigned by Phalcon, remove this annotation will do the work for you
I removed it and still same story. The id is whatever is assigned to it, it is not skipped.
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.
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?
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.