New relation self::CALC to support calculated attributes

It's easy enough to add a new 'calculated attribute' to a model by declaring a getNewAttrib() function.  The problem is that Yii doesn't know what information to query in support of this attribute.  It could be a simple relationship like 'name' comes from 'lastname' and 'firstname' in the same model.  Or it could be 'unitName' comes from 'Unit.name' in another model.  Or something more complicated involving MANY_MANY.

I propose a new CALC relation that can have a 'select' and a 'with' option.  The 'select' option is a list of which 'real attributes' in the same model to select for.  The 'with' option tells which attributes in other models will be needed. It will still be up to the getNewAttrib() function to actually do the calculation, of course, but all the information would be loaded.

I suppose this would require allowing CALC and STAT relations into a CDbCriteria.  Or perhaps we need a new CActiveCriteria that can take CALC and STAT attributes.  A function like eager() could return a CActiveFinder set up properly.

I want to use this to store reports in a way that the user can run them, and show/hide columns.

Thanks for considering it,

Greg

Why not implement a name() method and return the two parts concatenated? You can extend __get() if you still want to reach it via magic attribute ($model->name).The same applies to external data - why complicate it too much, when you can handle all the related information within methods even if they aren’t loaded eagerly.I don’t understand the whole idea of adding an entirely new type of relation, which would confuse newbies and create a maintainability nightmare for everybody.

Good question.  Here are my thoughts.

A CALC relation would ensure that the needed data is loaded and ready to use.  Concatenating might work at times.  But there might be things the view wants to do that requires the underlying columns.  Inline editing comes to mind.  A calculated attribute like Employee->projects() might return an array of projects.  The view can then display it however it wants, say as hyperlinked list.

This idea makes calculated attributes more of a first class object because Yii would know how to load the data.

best wishes,

Greg

Any relation would ensure data accessibility when eagerly loaded.

In my previous reply I suggested a bit complicated solution. After studying demo models, I figured out that one can implement getProject() method and call $model->project.

Sorry, if I misunderstood you at some point.