Yii Framework Forum: Problem with Sorting in CListView - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with Sorting in CListView Relational Attributes are not recognized Rate Topic: -----

#1 User is offline   nomar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 26-April 09

Posted 07 September 2010 - 05:17 AM

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
0

#2 User is offline   nomar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 26-April 09

Posted 07 September 2010 - 06:09 AM

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?
0

#3 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 25 October 2010 - 06:39 AM

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
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users