Sorting of related model of related model

Dear All,

I have a structure like this, FormHeader has many FormDetail and inside FormDetail there is referenceFk, which is actually refer to other FormDetail.

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(

            'formHeader' => array(self::BELONGS_TO, 'FormHeader', 'headerFk'),

            'reference' => array(self::BELONGS_TO, 'FormDetail', 'referenceFk'),



I want to sort the referenceFk based on its formNo which is in FormHeader.

I do it like this but always throw errors:

        $criteria = new CDbCriteria;

        $criteria->with = array('item', 'location', 'reference', 'reference.formHeader');

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

        $criteria->compare('itemFk', $this->itemFk);

        $criteria->compare('locationFk', $this->locationFk);

        $criteria->compare('type', $this->type);

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

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

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

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

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

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

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

        $sort = new CSort;

        $sort->defaultOrder = ' ASC';

        $sort->attributes = array(

            'id' => array(

                'asc' => ' ASC',

                'desc' => ' DESC',


            'itemFk' => array(

                'asc' => ' ASC',

                'desc' => ' DESC',


            'locationFk' => array(

                'asc' => 'location.warehouse ASC',

                'desc' => 'location.warehouse DESC',


            'type' => 't.type',

            'quantity' => 'quantity',

            'qtyConversion' => 'qtyConversion',

            'unitPrice' => 'unitPrice',

            'dispatchFee' => 'dispatchFee',

            'unit' => 'unit',

            'memoLine' => 'memoLine',

            'referenceFk' => array(

                'asc' => 'reference.formHeader ASC',

                'desc' => 'reference.formHeader DESC',




        return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

            'sort' => $sort,

            'pagination' => array(

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

                'currentPage' => Yii::app()->user->getState($curPage, 0),



Error is

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘reference.formHeader’ in ‘order clause’. The SQL statement executed was: SELECT AS t0_c0, t.headerFk AS t0_c1, t.itemFk AS t0_c2, t.locationFk AS t0_c3, t.type AS t0_c4, t.quantity AS t0_c5, t.qtyConversion AS t0_c6, t.unit AS t0_c7, t.unitPrice AS t0_c8, t.dispatchFee AS t0_c9, t.memoLine AS t0_c10, t.referenceFk AS t0_c11, AS t1_c0, item.code AS t1_c1, AS t1_c2, item.baseUnit AS t1_c3, item.weightPerUnit AS t1_c4, item.baseUnitPrice AS t1_c5, item.weightUnitPrice AS t1_c6, item.minStockLevel AS t1_c7, item.description AS t1_c8, item.categoryFk AS t1_c9, item.status AS t1_c10, AS t2_c0, location.code AS t2_c1, location.warehouse AS t2_c2, location.rack AS t2_c3, location.description AS t2_c4, location.status AS t2_c5, AS t3_c0, reference.headerFk AS t3_c1, reference.itemFk AS t3_c2, reference.locationFk AS t3_c3, reference.type AS t3_c4, reference.quantity AS t3_c5, reference.qtyConversion AS t3_c6, reference.unit AS t3_c7, reference.unitPrice AS t3_c8, reference.dispatchFee AS t3_c9, reference.memoLine AS t3_c10, reference.referenceFk AS t3_c11, AS t4_c0, formHeader.formType AS t4_c1, formHeader.headerNo AS t4_c2, formHeader.formDate AS t4_c3, formHeader.partnerFk AS t4_c4, formHeader.dueDate AS t4_c5, formHeader.referenceNo AS t4_c6, formHeader.truckNo AS t4_c7, formHeader.driver AS t4_c8, formHeader.deliveryCost AS t4_c9, formHeader.memo AS t4_c10, formHeader.postingStatus AS t4_c11 FROM form_detail t LEFT OUTER JOIN item item ON ( LEFT OUTER JOIN location location ON ( LEFT OUTER JOIN form_detail reference ON ( LEFT OUTER JOIN form_header formHeader ON ( WHERE (t.headerFk=:ycp0) ORDER BY reference.formHeader ASC, reference.formHeader ASC LIMIT 10

Can someone help?