how to call CModel::unsetAttributes() automatically when creating new model instance on "search" scenario?

I noticed that every newly created model instance will be populated with default values specified in the database automatically. But I don’t want this when creating new model instances on “search” scenario. Then how to call CModel::unsetAttributes() automatically on the newly created instance? Currently I put


right after every

$model=new ModelName('search');

. This is against the DRY principle apparently.

I think you could override method “afterConstruct”, but it’s only on CActiveRecord not CModel though.


My final solution:

  1. Add an "ActiveRecord" component which is extended from CActiveRecord, and add afterConstruct() to ActiveRecord as you said.

  2. Modify all models generated by Gii, extending them from ActiveRecord instead of CActiveRecord.