views:

302

answers:

4

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.

A: 

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'));
Mike
A: 

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

bancer
+2  A: 

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?

Travis Leleu
Unless I'm missing something, this wouldn't work because of how Cake decides whether to insert or update. From the manual, 3.7.4 Saving Your Data: "Creating or updating is controlled by the model's id field. If $Model->id is set, the record with this primary key is updated. Otherwise a new record is created." I've tried this and it is indeed how Cake operates.But yes, I'd like to set my IDs rather than using MySQL auto-increments. If possible, I'd also like to keep Cake from forcing auto-increment (but keep 'primary') on the field when the database is created from 'cake schema create'.
tomws
I should clarify: adding a *new* user-generated ID works. Attempting to create a new manifest with the same ID *updates* the previous. Perhaps that's rather a validation issue. Still, isUnique doesn't seem to be working on the ID field through the add form.
tomws
Right, isn't this the behavior you want? To be able to set your manifest ID when you create the record? Then you can set it in the same manner when you're editing an existing record. As far as the auto-generation stuff, I generally avoid that so I'm afraid I can't speak to it. Can't you just write an ALTER TABLE statement to remove the auto increment attribute in the table?
Travis Leleu
Good ideas. Sounds like I can make those work. Thanks!
tomws
+1  A: 

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?

Vicer