Problem with Sorting in CListView

Hi there,

I’ve been trying for a while now but I don’t get sorting with a related attribute to work. Probably I am missing something obvious.

My Model




public function relations() {

  return array(

      'articlenumberToSort' => array(self::HAS_ONE, 'ArticleNumber', 'media_id'),

   );

}



My Controller:




$sort = $sort = new CSort('Media');

$sort->attributes = array('*', 

'articlenumber' => array('asc' => 'articlenumberToSort.article_no','desc' => 'articlenumberToSort.article_no'));

        $dataProvider=new CActiveDataProvider('Media');

        $dataProvider->setSort($sort);

		$this->render('application.views.media.index',array(

			'dataProvider'=>$dataProvider,

            'sort' => $sort

		));



my view




<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

    'id'=>'media-view',

	'itemView'=>'application.views.media._view',

    'template' => '{pager} {summary} {sorter} {items} {summary} ',

    'sortableAttributes' => array('imagename', 'articlenumber'),

)); ?>



and my exception




 Column not found: 1054 Unknown column 'articlenumberToSort.article_no' in 'order clause'

Quelldatei


/opt/yii-1.1.3.r2247/framework/db/CDbCommand.php(377)


00365:             }

00366: 

00367:             if($this->_connection->enableProfiling)

00368:                 Yii::endProfile('system.db.CDbCommand.query('.$this->getText().')','system.db.CDbCommand.query');

00369: 

00370:             return $result;

00371:         }

00372:         catch(Exception $e)

00373:         {

00374:             if($this->_connection->enableProfiling)

00375:                 Yii::endProfile('system.db.CDbCommand.query('.$this->getText().')','system.db.CDbCommand.query');

00376:             Yii::log('Error in querying SQL: '.$this->getText().$par,CLogger::LEVEL_ERROR,'system.db.CDbCommand');

00377: throw new CDbException(Yii::t('yii','CDbCommand failed to execute the SQL statement: {error}',

00378:                 array('{error}'=>$e->getMessage())));

00379:         }

00380:     }

00381: }


Stacktrace:


#0 /opt/yii-1.1.3.r2247/framework/db/CDbCommand.php(266): CDbCommand->queryInternal('fetchAll', 2, Array)

#1 /opt/yii-1.1.3.r2247/framework/db/ar/CActiveRecord.php(1196): CDbCommand->queryAll()

#2 /opt/yii-1.1.3.r2247/framework/db/ar/CActiveRecord.php(1279): CActiveRecord->query(Object(CDbCriteria), true)

#3 /opt/yii-1.1.3.r2247/framework/web/CActiveDataProvider.php(123): CActiveRecord->findAll(Object(CDbCriteria))

#4 /opt/yii-1.1.3.r2247/framework/web/CDataProvider.php(120): CActiveDataProvider->fetchData()

#5 /opt/yii-1.1.3.r2247/framework/zii/widgets/CBaseListView.php(105): CDataProvider->getData()

#6 /opt/yii-1.1.3.r2247/framework/zii/widgets/CListView.php(151): CBaseListView->init()

#7 /opt/yii-1.1.3.r2247/framework/web/CBaseController.php(140): CListView->init()

#8 /opt/yii-1.1.3.r2247/framework/web/CBaseController.php(165): CBaseController->createWidget('zii.widgets.CLi...', Array)

#9 /home/ramon/projects/icd_media/protected/views/media/index.php(16): CBaseController->widget('zii.widgets.CLi...', Array)



Somehow the modelname is not replaced with the actual table, but also when I change it manually, the exception does not change, except for the tablename of cause.

Any suggestions appreciated.

Thanks,

Ramon

Looks like I found a solution, when creating the dataprovider I had to add my relation as well.




$sort = new CSort('Media');

        $sort->attributes = array('*', 'articlenumber' => array('asc' => 'articlenumberToSort.article_no','desc' => 'articlenumberToSort.article_no'));

        $dataProvider=new CActiveDataProvider('Media', array('criteria' => array('with' => 'articlenumberToSort')));



Sure I’ve done this before.

Anyway, is this the correct way to achieve this?

The problem Ramon is that you create an active provider of the model ‘Media’ but without its relations!

Do this my friend:





// $criteria is an CDbCriteria object in case you want to filter those results!

// the second parameter allows you to pass a CDbCriteria

$dataProvider = $dataProvider=new CActiveDataProvider('Media', array(

    'criteria'=>array(

        'with'=>array('articlenumberToSort'),

    )

));



I am sure it will work