根据用户ID获取用户名

时间:2015-12-19 17:48:45

标签: php mysql yii

我使用以下代码获取5个最活跃的用户ID。

 public function actionAllstats()
            {

        $this->layout='main';
        $user=UserJoinEvent::model()->findAll(array(//yeh hum model isi query mein chala raya hain because jab mein ne
                                                    //yeh $rbmodel=userjoinevent::model->findall() se kia tha to yeh mujhay saray results userjoinevent ke show raha 
                                                    //tha, matlab sari user_id of this table show kar raha tha
                'select'=>'user_id',
                'group'=>'user_id',
                'order'=>'COUNT(*) DESC',
                'limit' =>  5
             ));  
        $this->render('allstats', array("user"=>$user));
            } 

,视图文件为

<div>
        <?php
            foreach($user as $show)
            {
                echo '<h3>' . $show->user_id . '</h3>';
            }
        ?>
    </div>

如何才能获取用户名称,其名称来自用户模型。这是用户模型的代码

<?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 string $gender
 * @property string $activkey
 * @property string $create_at
 * @property string $lastvisit_at
 * @property integer $superuser
 * @property integer $status
 * @property string $salt
 * @property integer $requires_new_password
 * @property integer $login_attempts
 * @property integer $login_time
 * @property string $login_ip
 * @property string $activation_key
 * @property string $validation_key
 * @property string $create_time
 * @property string $update_time
 * @property string $reset_token
 * @property string $image
 * @property string $address
 *
 * The followings are the available model relations:
 * @property UserJoinEvent[] $userJoinEvents
 * @property UserRateReviewNgo[] $userRateReviewNgos
 * @property UserUploadVideo[] $userUploadVideos
 * @property UserWriteStory[] $userWriteStories
 * @property VolunteerForm[] $volunteerForms
 */
class User extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @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('superuser, status, requires_new_password, login_attempts, login_time', 'numerical', 'integerOnly'=>true),
            array('username, login_ip, address', 'length', 'max'=>45),
            array('password, email, activkey, activation_key', 'length', 'max'=>120),
            array('gender', 'length', 'max'=>1),
            array('salt, validation_key', 'length', 'max'=>255),
            array('reset_token', 'length', 'max'=>250),
            array('image', 'length', 'max'=>450),
            array('create_at, lastvisit_at, create_time, update_time', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, username, password, email, gender, activkey, create_at, lastvisit_at, superuser, status, salt, requires_new_password, login_attempts, login_time, login_ip, activation_key, validation_key, create_time, update_time, reset_token, image, address', 'safe', 'on'=>'search'),
               array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
                    //array('title, image', 'length', 'max'=>255, 'on'=>'insert,update'),
                    );
    }

    /**
     * @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(
            'userJoinEvents' => array(self::HAS_MANY, 'UserJoinEvent', 'User_user_id'),
            'userRateReviewNgos' => array(self::HAS_MANY, 'UserRateReviewNgo', 'User_user_id'),
            'userUploadVideos' => array(self::HAS_MANY, 'UserUploadVideo', 'User_user_id'),
            'userWriteStories' => array(self::HAS_MANY, 'UserWriteStory', 'User_user_id'),
            'volunteerForms' => array(self::HAS_MANY, 'VolunteerForm', 'User_user_id'),
                        //'profile' => array(self::BELONGS_TO, 'profile', 'id','through'=>'user'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            'gender' => 'Gender',
            'activkey' => 'Activkey',
            'create_at' => 'Create At',
            'lastvisit_at' => 'Lastvisit At',
            'superuser' => 'Superuser',
            'status' => 'Status',
            'salt' => 'Salt',
            'requires_new_password' => 'Requires New Password',
            'login_attempts' => 'Login Attempts',
            'login_time' => 'Login Time',
            'login_ip' => 'Login Ip',
            'activation_key' => 'Activation Key',
            'validation_key' => 'Validation Key',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'reset_token' => 'Reset Token',
            'image' => 'Image',
            'address' => 'Address',
        );
    }

    /**
     * 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->compare('id',$this->id);
        $criteria->compare('username',$this->username,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('gender',$this->gender,true);
        $criteria->compare('activkey',$this->activkey,true);
        $criteria->compare('create_at',$this->create_at,true);
        $criteria->compare('lastvisit_at',$this->lastvisit_at,true);
        $criteria->compare('superuser',$this->superuser);
        $criteria->compare('status',$this->status);
        $criteria->compare('salt',$this->salt,true);
        $criteria->compare('requires_new_password',$this->requires_new_password);
        $criteria->compare('login_attempts',$this->login_attempts);
        $criteria->compare('login_time',$this->login_time);
        $criteria->compare('login_ip',$this->login_ip,true);
        $criteria->compare('activation_key',$this->activation_key,true);
        $criteria->compare('validation_key',$this->validation_key,true);
        $criteria->compare('create_time',$this->create_time,true);
        $criteria->compare('update_time',$this->update_time,true);
        $criteria->compare('reset_token',$this->reset_token,true);
        $criteria->compare('image',$this->image,true);
        $criteria->compare('address',$this->address,true);

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

这是用户加入事件模型

<?php

/**
 * This is the model class for table "user_join_event".
 *
 * The followings are the available columns in table 'user_join_event':
 * @property integer $id
 * @property integer $user_id
 * @property integer $event_id
 * @property string $date_created
 * @property string $date_modified
 *
 * The followings are the available model relations:
 * @property Event $event
 * @property User $user
 */
class UserJoinEvent extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return UserJoinEvent 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_join_event';
    }

    /**
     * @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('user_id, event_id, date_created, date_modified', 'required'),
            array('user_id, event_id', 'numerical', 'integerOnly'=>true),
            array('date_created, date_modified', 'length', 'max'=>45),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, user_id, event_id, date_created, date_modified', 'safe', 'on'=>'search'),
        //http://stackoverflow.com/questions/18434575/to-store-datetime-automatically-using-yii
                    );
    }

    /**
     * @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(
            'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
            'user' => array(self::BELONGS_TO, 'User', 'user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'user_id' => 'User',
            'event_id' => 'Event',
            'date_created' => 'Date Created',
            'date_modified' => 'Date Modified',
        );
    }

    /**
     * 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->compare('id',$this->id);
        $criteria->compare('user_id',$this->user_id);
        $criteria->compare('event_id',$this->event_id);
        $criteria->compare('date_created',$this->date_created,true);
        $criteria->compare('date_modified',$this->date_modified,true);

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

三江源。

1 个答案:

答案 0 :(得分:0)

模型

$user=UserJoinEvent::model()->findAll(array(//yeh hum model isi query mein chala raya hain because jab mein ne
                                                    //yeh $rbmodel=userjoinevent::model->findall() se kia tha to yeh mujhay saray results userjoinevent ke show raha 
                                                    //tha, matlab sari user_id of this table show kar raha tha
                'select'=>'CONCAT(user_id," - ", user_name) as name',
                'group'=>'user_id',
                'order'=>'COUNT(*) DESC',
                'limit' =>  5
             ));  

查看

<div>
        <?php
            foreach($user as $show)
            {
                echo '<h3>' . $show->name . '</h3>';
            }
        ?>
    </div>