简体   繁体   English

PHP Yii:如果使用sqlite数据库,如何对CGridView行编号

[英]PHP Yii: how to row numbering CGridView if using sqlite database

I want to show a filter-able and sort-able column of row number in CGridView, but what i have got is only an empty sort-able (clickable header) and filter-able (it has filtering text box up there) column. 我想在CGridView中显示行号的可过滤和可排序列,但是我所得到的只是一个空的可排序(可单击标题)和可过滤(它具有过滤文本框)列。

In model (ActiveRecord) class i have added a new public attribute: 在模型(ActiveRecord)类中,我添加了一个新的公共属性:

public $number;

in the rules() function i have added: 在rules()函数中,我添加了:

public function rules()
{
    return array(
        ...
        ...
        array('
            ...
            ...
            number,
            ',
            'safe',
            'on'=>'search'
        ),
    );
}

on the search() function, i have also add: 在search()函数上,我还添加了:

public function search()
{
$criteria=new CDbCriteria;
...
...
$number_select = '( 
    select count(*)
    from tbl_mytable as k
    where k.id <= t.id
    )';
$criteria->select = array(
    $number_select . ' as number',
    't.*',
    );
...
...
$criteria->compare($number_select, $this->number,true);
$sort = new CSort;
$sort->attributes = array(
    '*',
    ...
    ...
    'number'=>array(),
    );

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

I've successfully using this kind of method to add other columns. 我已经成功使用这种方法添加其他列。 With the lack of skill and knowledge of sqlite query, i am suspecting the cause of this problem is the query in the $criteria->select . 由于缺乏sqlite查询的技能和知识,我怀疑造成此问题的原因是$criteria->select的查询。

I hope someone would help me for solving this. 我希望有人能帮助我解决这个问题。

Try adding the property to CActiveRecord.attributeNames : 尝试将属性添加到CActiveRecord.attributeNames

public function attributeNames()
{
    return array_merge(parent::attributeNames(), array('number'));
}

after editing my question, i edited my code a fad that miraculously turn out to be the answer. 在编辑完我的问题之后,我编辑了我的代码,这真是奇迹般地成为了答案。

Just remove the table alias from the select statement 只需从select语句中删除表别名

Original (not working) code: 原始(无效)代码:

$number_select = '( 
    select count(*)
    from tbl_mytable as k
    where k.id <= t.id
    )';

The new (working) code after removing table alias: 删除表别名后的新(工作)代码:

$number_select = '( 
    select count(*)
    from tbl_mytable
    where tbl_mytable.id <= t.id
    )';

While my question is about yii with sqlite database, but since this is quite standard query, i think this solution will also works for yii with mysql database 虽然我的问题是关于使用sqlite数据库的yii,但是由于这是非常标准的查询,我认为此解决方案也将适用于使用mysql数据库的yii。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM