potential new active record behavior features?

What do you think? Some of this may already be possible




<?php

//MyBehavior is attached to Model.  $model is an instance of Model


//attributeName is a public attribute of MyBehavior

$model->myBehavior->attributeName //With this method of referencing attributes, there are no naming collisions


//methodName is a public method of MyBehavior

$model->myBehavior->methodName()


//attributeName is an public attribute of myBehavior

CHtml::activeTextField($model, 'myBehavior.attributeName'); //html name = Model[myBehavior][attributeName]


$model->setAttributes($_GET['Model']) //injects attributes into the attached behaviors as well as the models


//MyBehavior can have methods similar to the model classes, such as attributeLabels(), relations() and rules() (and attributes can be marked as safe and unsafe)


//A relation implanted by a behavior could be reached with

$model->myBehavior->relationName


//label is pulled from attributeLabels() in the behavior class

CHtml::activeLabel($model, 'myBehavior.attributeName');


//An alternate method to using $model->myBehavior for namespace problems

$model->getBehavior('myBehavior')



This could be a huge task to implant… I’m just brainstorming here

PROTOTYPE 2

This is not much different from the current implementation.

This is a shortened version of prototype 1. The only new thing here is relations(), attributeLabels(), and rules() in the behavior classes




<?php

//MyBehavior is attached to Model.  $model is an instance of Model


//attributeName is a public attribute of MyBehavior

$model->attributeName


//methodName is a public method of MyBehavior

$model->methodName()


//attributeName is an public attribute of myBehavior

CHtml::activeTextField($model, 'attributeName'); //html name = Model[attributeName]


$model->setAttributes($_GET['Model']) //injects attributes into the attached behaviors as well as the models


//MyBehavior can have methods similar to the model classes, such as attributeLabels(), relations() and rules() (and attributes can be marked as safe and unsafe)


//A relation implanted by a behavior could be reached with

$model->relationName


//label is pulled from attributeLabels() in the behavior class

CHtml::activeLabel($model, 'attributeName');



All except relationName are already available.

Ok let me further minimize my proposal to three sentences:

  1. Allow relations to be defined in AR behaviors and be accessible like $model->relationID

  2. Allow rules() to be defined in AR behaviors that are applied to attributes within that behavior when the model is validated

  3. attributeLabels() in behaviors for labeling attributes in that behavior

If I am correct, these three things are not currently supported?

I have seen public behaviors that require an extra step in installation in that you have to define a relation to use them. It would be nice if those relations could simply be defined in the behavior

The reason I like $model->mybehavior->attributeName over $model->attributeName is that it is more obvious to me what is being accessed, and there is less chance of naming collisions. On the downside, it is less transparent. I know some people disagree with me on that one

  1. Related: http://code.google.com/p/yii/issues/detail?id=943

2 and 3: both make sense.

Regarding $model->mybehavior->attributeName and $model->attributeName, they are both supported. $model->attributeName stands for attributeName for behaviour defined first in the list.