Model Relation With Condition

Hi All,

I need help on this, I have two tables, form and form_status. Form can be in DRAFT, VERIFIED, FULFILLED or CANCELLED. Instead of having four fields for operator and four fields for timestamp and even 4 memo fields to record form status changes, I introduce form_status table.

in form_status, i have id, formFk, status, recordedBy, recordedAt, memo.

I defined the relations as




'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)),



The thing is this changes makes me not having values for old forms. I want them to be shown as ‘N/A’.

The problem is that I only have forms that has related rows in form_status shown. The rest are filtered out.

Below is my search function




public function search($type) {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria = new CDbCriteria;

        $criteria->with = array('formDetails');


        $criteria->compare('t.type', $type);

        $criteria->compare('formNo', $this->formNo, true);

        $criteria->compare('formDate', $this->formDate, true);

        $criteria->compare('partner.name', $this->partnerName, true);

        $criteria->compare('partner.name', $this->foremanName, true);

        $criteria->compare('partner.name', $this->supervisorName, true);

        $criteria->compare('t.memo', $this->memo, true);

        $criteria->compare('t.status', $this->status);


        $sort = new CSort;

        $sort->defaultOrder = 'formDate DESC, formNo DESC';

        $sort->attributes = array(

            'formNo' => array(

                'asc' => 'formDate ASC, formNo ASC',

                'desc' => 'formDate DESC, formNo DESC',

            ),

            'formDate' => 'formDate',

            'partnerName' => array(

                'asc' => 'partner.name ASC',

                'desc' => 'partner.name DESC'

            ),

            'foremanName' => array(

                'asc' => 'partner.name ASC',

                'desc' => 'partner.name DESC'

            ),

            'supervisorName' => array(

                'asc' => 'partner.name ASC',

                'desc' => 'partner.name DESC'

            ),

            't.memo' => 't.memo',

            't.status' => 't.status',

            'createdBy' => array(

                'asc' => 'formCreated.recordedBy ASC',

                'desc' => 'formCreated.recordedBy DESC',

            ),

            'createdAt' => 'createdAt',

        );


        $sort->applyOrder($criteria);


        $formType = FormType::model()->findByPk($type);


        return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

            'sort' => $sort,

            'pagination' => array(

                'pageSize' => Yii::app()->user->getState($formType->shortName . '_pageSize', Yii::app()->params['defaultPageSize']),

                'currentPage' => Yii::app()->user->getState($formType->shortName . '_page', 0),

            ),

        ));

    }



Please help me on understanding the relation.

Thank you in advance.

It turns out that I need to use the formStatus relation (HAS_MANY) one rather than the formCreated relation (HAS_ONE). Also need to add $criteria->together = true.

I don’t know the explanation though…but it works