Extend CActiveRecord for all models?
#1
Posted 17 February 2010 - 04:16 PM
The idea is to avoid having to edit every single model, as I'd rather edit 1 file than 90.
Thanks!
#2
Posted 17 February 2010 - 04:38 PM
http://www.yiiframew...on.use#behavior
#3
Posted 17 February 2010 - 05:30 PM
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
// ...
// application components
'components'=>array(
// ...
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=noneya',
'emulatePrepare' => true,
'username' => 'noneya',
'password' => 'noneya',
'charset' => 'utf8',
'behaviors'=>array(
'BlameableBehavior'=>array(
'class'=>'application.components.behaviors.BlameableBehavior',
'createdByColumn'=>'created_by',
'updatedByColumn'=>'modified_by',
),
'CTimestampBehavior'=>array(
'class'=>'zii.behaviors.CTimestampBehavior',
'createAttribute'=>'created',
'updateAttribute'=>'modified',
'setUpdateOnCreate'=>true,
),
),
),
// ...
),
// ...
);
#4
Posted 17 February 2010 - 05:44 PM
intel352, on 17 February 2010 - 05:30 PM, said:
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
// ...
// application components
'components'=>array(
// ...
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=noneya',
'emulatePrepare' => true,
'username' => 'noneya',
'password' => 'noneya',
'charset' => 'utf8',
'behaviors'=>array(
'BlameableBehavior'=>array(
'class'=>'application.components.behaviors.BlameableBehavior',
'createdByColumn'=>'created_by',
'updatedByColumn'=>'modified_by',
),
'CTimestampBehavior'=>array(
'class'=>'zii.behaviors.CTimestampBehavior',
'createAttribute'=>'created',
'updateAttribute'=>'modified',
'setUpdateOnCreate'=>true,
),
),
),
// ...
),
// ...
);
You specified two behaviors to be attached to the "db" application component. As mentioned in the guide, for CActiveRecord you have to override the behaviors() method. You can do that in a base controller you create, but you still have to change your models so they extend from your base controller.
(not tested)
/Tommy)
#5
Posted 17 February 2010 - 06:05 PM
tri, on 17 February 2010 - 05:44 PM, said:
(not tested)
/Tommy)
Thanks Tommy, but I was hoping to avoid that route, as that then requires that I extend the custom class in every model (means editing every model).
Does anyone have any thoughts on how to attach the behaviors to models on-the-fly (or a hack for the config) to easily add the behaviors?
-Jon
#6
Posted 17 February 2010 - 06:33 PM
intel352, on 17 February 2010 - 06:05 PM, said:
Does anyone have any thoughts on how to attach the behaviors to models on-the-fly (or a hack for the config) to easily add the behaviors?
-Jon
Have you looked at attachBehaviors ? http://www.yiiframew...ehaviors-detail
nz
#7
Posted 17 February 2010 - 07:53 PM
#8
Posted 18 February 2010 - 05:03 AM
cd protected/models sed -i 's/CActiveRecord/MyARBaseClass/' *.php
#9
Posted 18 February 2010 - 09:40 AM
notzippy, on 17 February 2010 - 06:33 PM, said:
Thanks notzippy, I'll check that out for future reference
qiang, on 17 February 2010 - 07:53 PM, said:
Mike, on 18 February 2010 - 05:03 AM, said:
cd protected/models sed -i 's/CActiveRecord/MyARBaseClass/' *.php
Thanks qiang and Mike, I went ahead and extended CActiveRecord, but I would like to go on the record as saying that being able to specify AR behaviors from main.php would be awesome ;-)

Help















