Kopezaur
Kopezaur

Reputation: 58

How can I send a variable from a controller to a behaviour? cakePHP

I have created a behaviour for my Tables which sets in the beforeSave() method two basic variables of a table: created_by and created_on(same for modified and deleted).

I currently use the active_user in my controllers, setting it from the beforeFilter() method of AppController. How can I send active_user->username from my controller to the behaviour? Is there a possible way? Or an alternative method of sending a string to the behaviour?

Upvotes: 0

Views: 175

Answers (2)

datalore
datalore

Reputation: 327

You can also use muffin/footprint plugin and customize which data you want to save in beforeSave.

Upvotes: 1

arilia
arilia

Reputation: 9398

Remember that cake has a builtin behavior to save the created and modified date. All you have to do enable it in the Table. (see the manual here)

 $this->addBehavior('Timestamp');

To save the even the user I use a plugin which set a global variable in the controller (you can do it in your AppController)

$authUser = $controller->Auth->user();
Configure::write('GlobalAuth', $authUser);

and in the behavior

$user_id = Configure::read('GlobalAuth.id');

But sincerely I don't know if this is the best method.

the plugin I use is this, for your information

https://github.com/cakemanager/cakephp-utils

Upvotes: 1

Related Questions