Yii Framework Forum: Extend CActiveRecord for all models? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Extend CActiveRecord for all models? Rate Topic: ***** 1 Votes

#1 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 17 February 2010 - 04:16 PM

Is it possible to extend CActiveRecord to add a behavior to be applied across all models?
The idea is to avoid having to edit every single model, as I'd rather edit 1 file than 90.

Thanks!
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#2 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 17 February 2010 - 04:38 PM

Ah, looks like this should suffice (editing the config to specify a behavior to be loaded)

http://www.yiiframew...on.use#behavior
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#3 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 17 February 2010 - 05:30 PM

Hmm, I was hoping to get it working as follows (by editing config/main.php), but apparently no go?

// 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,
                ),
            ),
        ),
        // ...
    ),
    // ...
);

Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#4 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,652
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 17 February 2010 - 05:44 PM

View Postintel352, on 17 February 2010 - 05:30 PM, said:

Hmm, I was hoping to get it working as follows (by editing config/main.php), but apparently no go?

// 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)
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 17 February 2010 - 06:05 PM

View Posttri, on 17 February 2010 - 05:44 PM, said:

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)


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
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#6 User is offline   notzippy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 06-October 08

Posted 17 February 2010 - 06:33 PM

View Postintel352, on 17 February 2010 - 06:05 PM, said:

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

Have you looked at attachBehaviors ? http://www.yiiframew...ehaviors-detail

nz
0

#7 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,907
  • Joined: 04-October 08
  • Location:DC, USA

Posted 17 February 2010 - 07:53 PM

I would also suggest to have your own base AR class. The change is very easy if you can do a global string replacement.
0

#8 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,017
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 18 February 2010 - 05:03 AM

If you have the oportunity to be on a linux console:

cd protected/models
sed -i 's/CActiveRecord/MyARBaseClass/' *.php

0

#9 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 18 February 2010 - 09:40 AM

View Postnotzippy, on 17 February 2010 - 06:33 PM, said:

Have you looked at attachBehaviors ? http://www.yiiframew...ehaviors-detail

nz


Thanks notzippy, I'll check that out for future reference

View Postqiang, on 17 February 2010 - 07:53 PM, said:

I would also suggest to have your own base AR class. The change is very easy if you can do a global string replacement.

View PostMike, on 18 February 2010 - 05:03 AM, said:

If you have the oportunity to be on a linux console:

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 ;-)
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users