【问题标题】:How to display query result in CGridView using DataProvider如何使用 DataProvider 在 CGridView 中显示查询结果
【发布时间】:2011-12-23 11:40:23
【问题描述】:

这 3 个表只是我整个项目的一部分(用户、配置文件、登录)

Login.php(登录表模型

public function topten_logins(){

    $criteria = new CDbCriteria;
    $criteria->select = 'concat(u.firstname," ",u.lastname) as Name, p.join_date as Joined, count(*) as Logins';
    $criteria->alias = 'l';
    $criteria->join = 'left join users u on (u.id = l.user_id) left join profile p on (p.user_id = l.user_id)';
    $criteria->group = 'l.user_id';
    $criteria->order = 'Logins desc';
    $criteria->limit = '10';

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

我要在CGridView中显示,我要显示的代码如下:

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

    'dataProvider'=>Login::model()->topten_logins(),
    'enablePagination' => false,
    'columns'=>array(
        'Name',
        'Joined',
        'Logins',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

我只想显示登录次数最多的前十名用户。

但它给了我错误信息:

未定义属性“Login.Name”。

我以前没用过 Yii。因此,我们将非常感谢您的帮助。

【问题讨论】:

    标签: gridview yii


    【解决方案1】:

    问题是您正在使用“作为名称和登录名”进行查询。在这种情况下,您需要做的是将“名称”和“登录”定义为模型中的变量。在模型的类定义中添加

    public $Name;
    public $Logins; 
    

    它应该可以工作。

    【讨论】:

    • 非常感谢,自从过去 14 小时以来,我一直在搞砸这 3 行。效果很好。
    • 嘿@redGREENblue,你能告诉我如何对此应用排序吗??
    【解决方案2】:

    尝试将函数“public function topten_logins()”移动到用户模型或为 CGridView 指定了所有列名的模型中。

    否则你可以将列数组设置为

    $column['name']=>get name from user table 
    $column['Joined']=>get Joined from related table 
    $column['Logins']=>get Logins from related table 
    

    然后设置

    'columns'=>$column
    

    在 CGridView 中

    【讨论】:

    • 感谢您的帮助,但它没有用,否则我无法清楚地理解它。@redGreenblue 的另一个建议很简单,告诉我在哪里设置列数组? ? (在函数内部或其他地方)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多