How To Attach Attribute To Model Via Cactiverecordbehavior




class Model extends CActiveRecord

{

   public $maxPos;

}


class PositionBehavior extends CActiveRecordBehavior

{

   function someMethod()

   {

       $owner = $this->getOwner();

       $criteria = new CDbCriteria;

       $criteria->select='MAX(position) as maxPos';

       $position = $owner::model()->find($criteria);

   }

}



If I want to get clear model class without any vars as $maxPos how can I attach public attribute $maxPos to CActiveRecord model from CActiveRecordBehavior class? It is possible? Or how I can get $position->maxPos variable without public property in Model? Thanks!

try declaring that variable maxPos in your behavior and then check whether this is working or not.

Behavior properties are reachable as ordinary model properties. The only issues you will face is that they’re not included in attributeNames(), rules() and attributeLabels() methods.

I’ve just recently made such behaviors in Yii 2 and had to add some generic methods to gather behavior attributes to return in those methods.

Thanks! When I trying to add public property to Behavior class I can’t see it in var_dump of Model, but property is exists and accessible.