1

I have a table that stores information about a role. In that table I have two fields that store the ids from other tables.

Table one: id, rname, pname, idtwo, idthree

Table two: id, twoname

Table three: id, threename

My ClistView displays rname, pname, twoname, threename

With this forums help I was able to use a CListView and display the names from tables two and three.

What I would now like to do is be able to do is create a view where the information is sorted on twoname and aonther view where the data is sorted on threename.

My actionIndex is:

public function actionIndex() {
$dataProvider = new CActiveDataProvider('Staffroleprofile');
$this->render('index', array(
'dataProvider' => $dataProvider,
));
}

and my _view.php is:

<h1>List of SRP's</h1>
<?php 
$dataProvider->sort->defaultOrder='rname ASC'; 
?>

<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>    

so the above gives me the data sorted by rname from table one. How would I specify that I want the data to be sorted by the data in table two or three?

Kind regards,

e25taki

4

2 回答 2

0

如果model2model3分别是表 2 和表 3 的关系Staffroleprofile,请将其添加到您的代码中

$dataProvider = new CActiveDataProvider('Staffroleprofile');
$criteria=new CDbCriteria;
$criteria->together=true;
$criteria->with=array('model2','model3');
$dataProvider->criteria=$criteria;
$dataProvider->sort->defaultOrder='model2.twoname ASC';

CDbCriteria::together指定是否应使用连接运行查询。如果设置为 false,则在需要关系时为每条记录运行附加查询。有关更多详细信息,请访问CDbCriteriaCActiveDataProvider的 API 页面

于 2013-06-09T13:56:31.493 回答
0

现在一切都好,我需要的代码是:

$dataProvider = new CActiveDataProvider('Staffroleprofile');
$criteria=new CDbCriteria;
$criteria->together=true;
$criteria->with=array('sitename','businessname');
$dataProvider->criteria=$criteria;
$dataProvider->sort->defaultOrder='sitename.site ASC';
$this->render('index_bysite', array(
        'dataProvider' => $dataProvider,
));

因此,您可以从 Staffrleprofile 模型中获得记录的句柄,然后添加排序标准,在本例中它是基于关系的。谢谢大家的帮助。我的 Yii 学习冒险又迈出了一小步。

干杯!

e25taki

于 2013-06-11T20:40:22.513 回答