save() returns true but not saved?

I’m trying to figure out what could be the reason for this exiting problem:

Im saving a row with save() functions which returns true.

After save() function, if I call var_dump($model->id), it prints every time id number, which is

1 more than before(as It has to do becouse there is auto_increment in MySQL database.)

Also I inserted manually one row in phpmyadmin to my table, I gave it id 325. After doing save() operations many times, there are no more rows in table

but every time I call var_dump($model->id) it is printing like 326, 327…354, 355… and so on.

So, the auto_increment is working like there would be more rows, but there are just only one which I inserted manually.

I’m thinking could it be problem with MySQl better than Yii?

I have other tables which are working correctly with save() function. Also the ActiveRecords should be correctly generated and in date.

I someone has intelligence to give me ideas, I would be very pleased.

You need to understand how auto incrementing works in mysql.

The internal counter of the table keeps the highest value and increases it by one.

For instance, if you have 1000 rows in database, then you drop them all, next inserted row will have the id 1001 NOT 1.

Also, that counters increment independetly if the sql row is inserted or not. Try to see the generated sql and use that one in mysql and see what errrors throw.

Hey Thanks, i got it solved,

actually it got broken by db-transaction try what I just copied without knowing how does it work:




$transaction = Yii::app()->db->beginTransaction();

try 

{


  ***Here was my code




} catch(Exception $e) 

{

	var_dump($e);

        Yii::app()->end();

        $transaction->rollBack();

}



The catch part never launched but still save() function didnt work with code above.

Sounds like you didn’t commit the transaction.

that’s absolutely true, thanks!

Hi Poll. (and everyone)

I have a problem when use ->rollBack (in beginTransaction). save of model canceled (as expect that) but the next time the new record (that succesfully saved) has very next id.

for example if the last record has id=100 and we use rollback for new record, the next saved record will has id=102 (and not 101)

whats wrong?? (obviously rollback fuction delete the saved record)

How prevent that? I want the new successfully record has the appropiate id.

Thanks in Advance!

MySql does that

If you really need it, you could set an ID manually to it

Thanks Gustavo for really quick reply!

Is threre way to set auto current id by Yii function or I have to do from pure php code?

Thanks

Hey Kona

Yea, that would be by php, knowing the last ID and set normal yii way like:




$model= MyModel::model();

$model->property=$lastId+1;

$model->otherProperty='otherValue';

$model->save();



and how you will get the last ID you choose thebest way according to your application

Hi Gustavo

I think mysql do not allow us to set an id of any record to be less of auto_increment value

I found another solution:

//set auto_increment to our desire

$a=Yii::app()->db->createCommand(‘ALTER TABLE tbl_mymodel AUTO_INCREMENT=’.(int)($lastId+1));

$a->execute();

then we can save mymodel as mymodel->save() with sequential ids

anyway… thank you in appreciate :)

I think that is a bad idea to rely on php for autoincremented id.

What will happen if 2 user are saving in the same time? There is a small possibility that they will read the same value and save the same id.

Better to rely on mysql autoincrement and tolerate holes, what can they do of wrong?

Agreed , but the chance of that happen is less that 0.01% for a normal application, which is less than the % of some other occurs

but the following would be better:

right after the rollBack do this query


 $a=Yii::app()->db->createCommand('ALTER TABLE tbl_mymodel AUTO_INCREMENT=AUTO_INCREMENT-1');

I’m also curious about why not leave any empty field tho

Cheers

Hi Zaccaria.

You Have right… Yes

the basic problem is with beginTransaction(). When we canceled any changes with rollBack() then increase the auto_increment value.

I want to use rollBack() without any changes of auto_increment.

Can help with that? any suggestion?

Thanks

You optimized my code, Gustavo

zaccaria is right about the possibilities (they are not many but they exist), you have minimized them but if a web site has too many traffic the problem continues.

I think that depends from the mysql. if we attempt to change auto_increment while two or more users save records then auto_increment does not change.