Self deleting model rows by Yii?

Hi I was hardly coding my app over night. Yesterday I noticed that sometimes Yii inserts multiple rows on model->save()

Today I opened my app and noticed that some rows were deleted.

PhpMyAdmin screenshot (missing autoincrementing IDs 1000003 1000004 1000006)

My app is on ISP hosting server so I don’t have mysql nor apache logs. Deleting rows is not logged in yii application.log I think…

Anybody had similiar cases? I know that multiple inserts sometimes happened, what about deletions?

I guess that this is the answer… during night (expecially in the darkest hours) happens the more unusual and unbelivable stuffs…

Don’t be afraid, such a night gosth disappear at the first light of the day (and after good sleeping and good breakfast with strong tea)

It ain’t funny man. To delete row from Yii I would have to click on CButtonColumn of cGridView and the confirm alert. No way I did that. Deleting row from database by myself? Why?

Well yesterday at the afternoon the same thing happened, anyway I thought "maybe not…" but I knew it was there and then gone.

Any foreign relations set up to cascade by chance?

Yep that could do it!

doodle

None. I checked DB sql from PhpMysqlAdmin export, and Yii’s models code. Well I would remember… That’s sick - I added missing rows with missing IDs, now after few hours ID 1000001 is gone. What the heck? I’ll check awstats on my ISP site, mysql logs are beyond my eyes…

Would Yii log deletion of the model row?

Hm, you could add a beforeDelete method for the model which will explicitly call Yii::log() and log the deletion of the item. If your problem is from within Yii, you’ll at least have a record of it.

I tried to put this code in model (and in controller to try…) after this post example:




protected function beforeDelete()

{

Yii::log();

return parent::beforeDelete();

}



but what I get is this small animated circle:

How can I fix this?

Yii::log(); is very simple usage :) You need to write something like this:


Yii::log('row with id '.$this->id.' will be deleted');

Well I thought that if I don’t put any text, some default will be logged.

Where should I put this beforeDelete() method - in model or controller?

I put this code in model:




protected function beforeDelete()

{

Yii::log('row with id '.$this->id.' will be deleted');

return parent::beforeDelete();

}



but still getting this animated "wheel" and nothing else

In model.

So I did…

If you use Firefox+Firebug view the result of AJAX query or view application.log file in protected\runtime, may be there are any errors.

This is the syntax you need to use for the log:

http://www.yiiframework.com/doc/api/1.1/CLogger#log-detail




public void log(string $message, string $level='info', string $category='application')



So, in the model, something like:




protected function beforeDelete()

{

    Yii::log('Yii is deleting this model: '.$this->id, 'warning', 'application.models');

    return parent::beforeDelete();

}



Are you sure that ‘this->id’ is set ?

That the spinner just spins indicates an error in that function.

Most likely ‘id’ is not set.

Try it without the id first. :)

Rather than testing it from the admin view, I would recommend testing it from the ‘view’ first, if you have one set up – less external variables to worry about.

Then it’s trying to delete a row that doesn’t exist?

I tried without any id just to log any text (function is put in model).




public function beforeDelete()

{

Yii::log('row with id will be deleted'); // '.$this->id.'

return parent::beforeDelete();

}



Spinner still spins… I even tried to do: return 1; instead of Yii:log(‘blahblahblah’); - no change

Have you checked that i wrote in post #14?