0

For example: I create new model, set the id field to 1 and save it. It runs fine and saves with the id = 1 into a database. I open MySQL console and delete all records from the table. When I delete it, create new model again and set the id field to 1 - it actually saves it with the 2 as the id field value.

I'm guessing Yii gets the current auto_increment value from mysql and override my id value. Is there a way to prevent that behavior?

EDIT (my code sample):

$sn = new SaplingNode();
$sn->id = 1;
$sn->save();

I call it twice, between calls I delete the record using mysql console. That's all.

Joe
  • 2,551
  • 6
  • 38
  • 60
  • It would be ridiculous for Yii to reimplement auto-increment on its own, the DB is the right place to handle that. If anything, it's more likely Yii is *not assigning your specific value* to the primary key field and letting auto-increment do its job, but without your code it's impossible to tell if that is really the case. – DCoder Dec 07 '13 at 16:45
  • @DCoder - I don't want Yii to reimplement auto-increment but to make simple insert with id specified by me. I added the code but it's not relevant actually as it is simple creating and saving model. – Joe Dec 07 '13 at 17:04

5 Answers5

2

There is no way to do this by Yii framework. As its a default behavior of MYSQL.

You can simply do this and reset the Auto increment id after you truncate.

ALTER TABLE tablename AUTO_INCREMENT = 1
dev1234
  • 5,376
  • 15
  • 56
  • 115
  • That's not strictly true. Mysql will autincrement if no id is specified in an insert query. You can specify a value for the id, and mysql will use that. It's not ideal though, and Yii doesn't support this method unless you write your own update query. – Joe Miller Dec 13 '13 at 20:27
  • Perfect answer (+1)! Side comment / question: [Is there any way to do this in migration, using `CDbMigration`](http://stackoverflow.com/q/27402950/1469208)? – trejder Dec 10 '14 at 14:12
2

This is not a best practice, but if you really want to forcefully set the id of new record on auto increment column, you can do like below:

First under your rules() method of "SaplingNode" model put below line:

array('id', 'safe'),

Then use your code to save new record with whatever id you like, for example:

$sn = new SaplingNode();
$sn->id = 10;
$sn->save();

This code should work, I have tested it.

dev1234
  • 5,376
  • 15
  • 56
  • 115
Yohan Hirimuthugoda
  • 1,053
  • 3
  • 11
  • 20
1

As you are currently using it, the answer is no, you can't do that, not with CActiveRecord. The reason for this is that Yii is retrieving the record to update based in it's primary key, and will not override that. The only way to override the primary key will be to write your own update query via a CDbCommandBuilder.

BTW: There is an interesting discussion on the subject, on Yii forum.

trejder
  • 17,148
  • 27
  • 124
  • 216
Joe Miller
  • 3,843
  • 1
  • 23
  • 38
  • Actually I didn't update it. I created a whole new object and just set the ID before saving it. I was expecting it to save it with my id but it didn't. Thank you for the link anyway, it's indeed interesting discussion. I thought so that I'll need to use command builder finally. Thanks. – Joe Dec 08 '13 at 19:13
0

Have you tried

$sn = new SaplingNode();
$sn->setPrimaryKey(1); 
$sn->save();

Yii primary key

devBinnooh
  • 611
  • 3
  • 12
0

If your just testing and it is okay to delete all data from your table, you can empty the table. For example, you can use phpMyAdmin, click your DB to view its tables, find the table you want and click Empty showing on that table row. You will get a confirmation message with a choice of enabling or disabling FK, click OK and all data on that table will be deleted and your auto increment id will start from 1 when saving or inserting new record.

Miloud Eloumri
  • 779
  • 1
  • 8
  • 14