CakePHP:如何禁用 Model.id 上的自动增量?
CakePHP 1.3.0,mysqli
我有一个模型 Manifest,其 ID 应该 是打印表单中的唯一编号。然而,将 Manifest.id 设置为主键后,CakePHP 通过在字段上设置自动增量来帮助我。有没有办法通过 schema.php 和/或其他地方标记字段以禁用自动增量?我只需要一个普通的旧主键,没有它。
我能想到的唯一其他解决方案是添加一个单独的清单编号字段并更改六个其他表中的外键。有点浪费而且不那么直观。
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 manifest number field and changing foreign keys in a half dozen other tables. A bit wasteful and not as intuitive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该能够在添加表单中包含 ID 字段。只需确保覆盖其默认类型,否则 CakePHP 会将其变成隐藏字段。
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.
我对此有点好奇。当然,如果没有设置主键(在您的情况下是 ID),您就无法保存记录。因此,如果您不希望 ID 自动递增,那么您必须保存自己的 ID。而且它不应该造成问题。
你使用的是mysql数据库吗?如果是这样,您是否将 ID 字段上的 auto_increment 设置为 true ?那么每当你保存一条记录时,mysql本身就会自动增加ID,不是吗?
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?
您可以在清单模型中设置架构: http://book.cakephp.org/view/ 442/_schema。
You can set your schema in the Manifest model: http://book.cakephp.org/view/442/_schema.
我刚刚在我的蛋糕沙箱上测试了这个,它起作用了。
您所需要做的就是在要保存的数据中设置 id 字段。因此,如果您要保存发布数据,并且希望 id 为 200(任意;您可以为此源使用另一个表字段或用户输入或任何其他内容)。
这就是您所追求的,能够为 id 设置自己的值而不是自动递增它们吗?
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).
Is that what you're after, being able to set your own values for id's rather than auto incrementing them?