开发者

CakePHP: How can I disable auto-increment on Model.id?

CakePHP 1.3.0, mysqli

I have a model, Manifest, whose ID should be the unique number from a printed form. However, with Manifest.id set as the primary key, CakePHP is helping me by setting up auto-increment on the field. Is there a way to flag the field via schema.php and/or elsewhere to disable auto-increment? I need just a plain, old primary key without it.

The only other solution I can imagine is adding on a separate man开发者_运维问答ifest number field and changing foreign keys in a half dozen other tables. A bit wasteful and not as intuitive.


I just tested this out on my cake sandbox and it worked.

All you need to do is set the id field in the data you're saving. So, if you're saving post data, and you want the id to be 200 (arbitrary; you could use another table field or user input or anything else for this source).

$this->data['Manifest']['id'] = 200;

Is that what you're after, being able to set your own values for id's rather than auto incrementing them?


You should be able to include the ID field in the add form. Just make sure to override its default type or CakePHP will turn it into a hidden field.

echo $this->Form->input('id', array('type' => 'text'));


I am a bit curious about this. Of course you cannot save a record without the primary key (which is ID in your case) set. So if you don't want the ID to be incremented automatically, then you must be saving your own ID. And it shouldn't cause a problem.

Are you using a mysql database? If so, do you have the auto_increment set to true on the ID field? Then mysql itself will automatically increment the ID whenever you save a record won't it?


You can set your schema in the Manifest model: http://book.cakephp.org/view/442/_schema.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜