Where to add code that will execure for all models

Hi All,

I am newbie here, so not sure if this is basic question.

I have one database, where in all tables i have placed createdDate, updatedDate, createdIp, updatedIp fields.

Using CRUD i have generated all admin pages. Now above 4 fields are common in all models.Whenever admin add/edit any record these 4 field should be automatically added/updated. I don’t want to set it’s value in all pages.

Where can i add such code to auto capture these 4 values?

Please guide.

Thanks,

Vibha.

if you use an CActiveRecord for the model class then you can set the values of this fields in the inbuilt event methods beforeSave() or afterValidat()

alternatively create a class that extends CActiveRecord (place it in the components directory, IMHO) while all Model classes will extend from your custom class

and override this methods i mentioned above

or have a look at this article

cheers.

If you want to reuse this "auto set dates" feature also in other projects (but also if not ) I would suggest to use the CTimeStampBehavior.

Thanks both for your reply.

@binkabir: I have gone through the article and see how to use beforeSave() function. Now i need to write beforeSave function for all models. How can i write one common function among all models? I think i can do this by what you said:

[i]alternatively create a class that extends CActiveRecord (place it in the components directory, IMHO) while all Model classes will extend from your custom class

and override this methods i mentioned above[/i]

I have created AutoFillColumn.php file under component folder:


<?php

class AutoFillColumn extends CActiveRecord

{

	private $_model=null;

	public function beforeSave()

    {

	   $_model->createdDate = new CDbExpression('NOW()');	 

	   return parent::beforeSave();

    }

}

?>

Added it in config file:


'autoFill'=>array('class'=>'AutoFillColumn'),

Now what other change i need to do to auto update all "createdDate" field?

I think you don’t need to write your AutoFillColumn class to config cause it is auto loaded if it is located at the normal place for models (/protected/models[/subfolder])

You have to inherit all your models from your AutoFillColumn and you have to declare a beforeSave method in each model and call parent::beforeSave().




class ModelA extends AutoFillColumn

{

	public function beforeSave()

	{

 		return parent::beforeSave();

	}

}



@VibhaJ, kokomo have said it all in the last post. (follow it)

@kokomo: Thanks a lot! ;) It works and i am using it in my whole project.

During R&D i have read somewhere to create component for this.I have checked component in document and wiki but didn’t understand practically.

Can you please hint me what is exact use of component and give me one small example where i need to use component?

No problem

I’m not sure how a component can help you for this problem. Maybe they mean behavior but call it component…

The other way to solve your problem is to attach the CTimestampBehavior to your parent model fom which you inherit all other models. You can also write your custom behavior if needed.

The behaviour solution is more modular so you can simply attach the behavior wherever you need it and don’t have to write a “empty” beforeSave() method in each model.