How can I include a calculated column

I was wondering, what the best approach is to include calculated data when using the CActiveRecord approach?

I have a model class that inherits from CActiveRecord, that needs extra data which can be retrieved like this:




Booking::model()->findByAttributes(array('group_id'=>$this->id))->created



It basically gets the point in time when the first "Booking" entry was made, that references the current record.

However, that has very low performance, and I would like to know, if it is possible to include the information when the model is fetched/created to begin with? I can not just include the other table either, as it holds way more records and data than what I can retrieve here.

Any good ideas on how to go about this?

I am not sure whether this is the same case. I have a form and since I need to see the status of the forms, I made the form and form_status tables a one-to-many relation. One form can have many form_status.




 public function relations() {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'formDetails' => array(self::HAS_MANY, 'FormDetail', 'headerFk'),

            'numOfDetails' => array(self::STAT, 'FormDetail', 'headerFk', 'defaultValue' => 0),

            'formType' => array(self::BELONGS_TO, 'FormType', 'type'),

            'formStatus' => array(self::HAS_MANY, 'FormStatus', 'headerFk'),

            'formCreated' => array(self::HAS_ONE, 'FormStatus', 'headerFk', 'order' => 'recordedAt DESC', 'condition' => 'formCreated.status = :status', 'params' => array(':status' => FormHeader::DRAFT)),

            'formVerified' => array(self::HAS_ONE, 'FormStatus', 'headerFk', 'order' => 'recordedAt DESC', 'condition' => 'formVerified.status = :status', 'params' => array(':status' => FormHeader::VERIFIED)),

            'formCancelled' => array(self::HAS_ONE, 'FormStatus', 'headerFk', 'order' => 'recordedAt DESC', 'condition' => 'formCancelled.status = :status', 'params' => array(':status' => FormHeader::CANCELLED)),

            'partner' => array(self::BELONGS_TO, 'Partner', 'partnerFk'),

        );

    }



See that I can have all the status of a form, but I also define a relation to created, verified and cancelled form_status. Hence, I can quickly retrieve the info I need. Performance wise, in my case, there is no performance problem.

Hope can help.