views:

16

answers:

2

Im writing ClearCache behavior.

It's purpose is to delete some of custom cache files on every afterSave and afterDelete event of the model.

In order to delete right files i need to know name of controller and the name of action that called ModelWithClearCacheBehavior->save() or ModelWithClearCacheBehavior->delete()

My question is: How to get those names inside behavior?

A: 

There is no an elegant solution about this (at least I don't know it).

You can do it with a Configure::write class for example:

in your AppController's beforeFilter() you can add the following code:

Configure::write('current_controller', $this->name);
Configure::write('current_action', $this->action);

later on in your behavior you can access them with

Configure::read('current_controller');
Configure::read('current_action');

You can access it because you set them before any model iterations.

For sure it's not elegant but it's working.

Nik
A: 

Not something I've really done anything with, but a brief reading of the book seems to indicate that the model is (or should be) available inside the behaviour -

When creating behavior methods you automatically get passed a reference of the calling model as the first parameter. All other supplied parameters are shifted one place to the right.

You should then be able to access the model via $Model

Leo
Sorry, my brain was somewhere else. You're not bothered about the model.
Leo
I think I would probably end up doing something like Nik suggested but I would be more inclined to write a session variable.
Leo

related questions