Soft Delete

I’ve created a method in my base CActiveRecord class that performs a soft delete when applicable.

This works fine on my local server (LAMP).

But on the live server it works when I delete a single model IE:




$myModel = MyModel::model()->findByPk(1);

$myModel->delete(); //Performs a soft delete



But when it loops through the relations it tries to perform a MySQL DELETE on them

rather than following the beforeDelete logic.

Any idea why this is happening?

Just a note as well; the live server has very strict user privileges, the user has only the following permissions on each table:

  • INDEX

  • INSERT

  • SELECT

  • UPDATE

Try outputting some debugging text to ensure that your beforeDelete() method is being called for the child relations, and if so, why $this->hasAttribute(‘deleted’) is returning false. I’d guess that your method is simply not being called.

I take it that your child AR classes are definitely extending the correct AR base class and that you haven’t over-ridden beforeDelete() anywhere else in the class hierarchy between the two classes.

The change you introduce is in the "base active record class" so in the specific AR classes, make sure their beforeDelete() (I assume that they have such a method), they call their parent beforeDelete(), like this:




//last line of code in beforeDelete()

return parent::beforeDelete();

I added a Yii::log to the beforeDelete on my local machine, it fired every time fine.

On the server it just fired on the first model (none of the relations). The server files are identical (except config).

None of my models override beforeDelete.

The reason i return false and not parent::beforeDelete() is because I don’t want it to actually delete.

I’ll rephrase more accurately:

Say you have SomeClass that extends your base AR class BaseClass. BaseClass itself extends CActiveRecord. This is the situation I understood from you. Assuming that I’m right, since the soft delete method is in BaseClass you need to make sure that if you have an overriding beforeDelete() in SomeClass, it calls its [size=2]parent[/size][color=#666600][size=2]::[/size][/color][size=2]beforeDelete() as described previously. I think that you already checked this but for the small chance you haven’t - consider this a reminder :)[/size]

I can’t think of a reason for the server and local machine behaving differently. Is it possible that the child classes have had their executable code previously cached by a compiler?

It might be worth posting the differences in your configuration files if configuration is the only thing that changes between the local machine and server.

I have reworked the config files and now the only difference is the DB credentials.

I have disabled all caching (including schema caching).

It seems to make it through one level of child relations now, but then after it ignores the beforeDelete. Very Strange.