jiege 2011-10-18 09:32:17 4793次浏览 5条回复 0 0 0
<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property integer $sex
 * @property integer $integral
 * @property string $reg_time
 * @property string $last_login
 * @property string $last_ip
 * @property integer $user_rank
 * @property integer $createtime
 * @property integer $active
 * @property integer $points
 */
class User extends CActiveRecord
{
    public $nums;
    /**
    * Returns the static model of the specified AR class.
    * @return User the static model class
    */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
    * @return string the associated database table name
    */
    public function tableName()
    {
        return 'user';
    }

    /**
    * @return array validation rules for model attributes.
    */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id, username, createtime', 'required'),
            array('id, sex, integral, user_rank, createtime, active, points', 'numerical', 'integerOnly'=>true),
            array('username, email', 'length', 'max'=>60),
            array('password', 'length', 'max'=>120),
            array('last_ip', 'length', 'max'=>15),
            array('reg_time, last_login', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, username, password, email, sex, integral, reg_time, last_login, last_ip, user_rank, createtime, active, points, nums', 'safe', 'on'=>'search'),
        );
    }

    /**
    * @return array relational rules.
    */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'docsCount' => array(self::STAT, 'Docs', 'user_id'),
        );
    }

    /**
    * @return array customized attribute labels (name=>label)
    */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'username' => Yii::t('main','Username'),
            'password' => 'Password',
            'email' => Yii::t('main','Email'),
            'sex' => 'Sex',
            'integral' => Yii::t('main','Integral'),
            'reg_time' => 'Reg Time',
            'last_login' => 'Last Login',
            'last_ip' => 'Last Ip',
            'user_rank' => 'User Rank',
            'createtime' => Yii::t('main','Createtime'),
            'active' => 'Active',
            'points' => Yii::t('main','Points'),
            'nums' => Yii::t('main','DocsNums'),
        );
    }

    /**
    * Retrieves a list of models based on the current search/filter conditions.
    * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
    */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria=new CDbCriteria;
        $criteria->with = array('docsCount');
        $criteria->compare('docsCount',$this->nums, true);
        $criteria->compare('t.id',$this->id);
        $criteria->compare('username',$this->username,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('sex',$this->sex);
        $criteria->compare('integral',$this->integral);
        $criteria->compare('reg_time',$this->reg_time,true);
        $criteria->compare('last_login',$this->last_login,true);
        $criteria->compare('last_ip',$this->last_ip,true);
        $criteria->compare('user_rank',$this->user_rank);
        $criteria->compare('createtime',$this->createtime);
        $criteria->compare('active',$this->active);
        $criteria->compare('points',$this->points);
        $sort = new CSort();
        $sort->defaultOrder = 't.id DESC';
        $sort->attributes = array(
            'id' => array('asc'=>'t.id','desc'=>'t.id DESC'),
            'username'=> array('asc'=>'t.username','desc'=>'t.username DESC'),
            'email'=> array('asc'=>'t.email','desc'=>'t.email DESC'),
            'integral'=> array('asc'=>'t.integral','desc'=>'t.integral DESC'),
            'points'=> array('asc'=>'t.points','desc'=>'t.points DESC'),
            'nums'=> array('asc'=>'docsCount','desc'=>'docsCount DESC'),
            'createtime'=> array('asc'=>'t.createTime','desc'=>'t.createTime DESC'),
        );
        $sort->applyOrder($criteria);

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

视图代码:

<h2>用户查看</h2>
<?php 
$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=>$model->search(),
    'itemsCssClass'=>'fullwidth formtab',
    'filter'=>$model,
    'columns'=>array(
        'id',
        'username',
        //'password',
        'email',
        //'sex',
        array(
            'name'=>'nums',
            'value'=>'$data->docsCount',
        ),
        'integral',
        //'reg_time',
        //'last_login',
        //'last_ip',
        //'user_rank',
        'points',
        'createtime:datetime',
        //'active',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); 
?>

[attach]419[/attach]

[attach]420[/attach]

[attach]421[/attach] 执行的SQL语句。

我想对自定义的nums字段进行排序和搜索查询,但是页面排序执行后页面程序报错,求教原因!

您需要登录后才可以回复。登录 | 立即注册