根据用户的 id 获取用户的名称 [英] Get the name of the user against their id

查看:71
本文介绍了根据用户的 id 获取用户的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来获取 5 个最活跃的用户 ID.

 公共函数 actionAllstats(){$this->layout='main';$user=UserJoinEvent::model()->findAll(array(//yeh hum model isi query mein chala raya hain 因为 jab mein ne//yeh $rbmodel=userjoinevent::model->findall() se kia tha to yeh mujhay saray 结果 userjoinevent ke show raha//tha,该表的matlab sari user_id show kar raha tha'选择'='=>'user_id','组'=>'user_id','订单'=>'COUNT(*) DESC','限制' =>5));$this->render('allstats', array("user"=>$user));}

视图文件是

<?phpforeach($user 作为 $show){回声'<h3>'.$show->user_id .'</h3>';}?>

如何根据用户的 id 获取用户的名称,名称来自 User 模型.这是用户模型的代码

true),数组('用户名,login_ip,地址','长度','max'=> 45),数组('密码,电子邮件,激活密钥,激活密钥','长度','最大'=> 120),数组('性别','长度','最大'=> 1),数组('盐,validation_key','长度','max'=> 255),数组('reset_token', 'length', 'max'=>250),数组('图像','长度','最大'=> 450),数组('create_at, lastvisit_at, create_time, update_time', 'safe'),//以下规则由 search() 使用.//请删除那些不应搜索的属性.数组('id,用户名,密码,电子邮件,性别,activkey,create_at,lastvisit_at,超级用户,状态,salt,requires_new_password,login_attempts,login_time,login_ip,activation_key,validation_key,create_time,update_time,reset_token,图像,地址','安全', 'on'='搜索'),数组('图像', '文件', 'types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),//array('title, image', 'length', 'max'=>255, 'on'=>'insert,update'),);}/*** @return 数组关系规则.*/公共函数关系(){//注意:您可能需要调整关系名称和相关的//下面自动生成的关系的类名.返回数组('userJoinEvents' =>数组(自我:: HAS_MANY,'UserJoinEvent','User_user_id'),'userRateReviewNgos' =>数组(自我:: HAS_MANY,'UserRateReviewNgo','User_user_id'),'userUploadVideos' =>数组(自我:: HAS_MANY,'UserUploadVideo','User_user_id'),'userWriteStories' =>数组(自我:: HAS_MANY,'UserWriteStory','User_user_id'),'志愿者表格' =>数组(自我:: HAS_MANY,'志愿者表格','用户用户ID'),//'个人资料' =>数组(self::BELONGS_TO,'profile','id','through'=>'user'),);}/*** @return 数组自定义属性标签(name=>label)*/公共函数attributeLabels(){返回数组('id' =>'ID','用户名' =>'用户名','密码' =>'密码','电子邮件' =>'电子邮件','性别' =>'性别','活动密钥' =>'激活键','create_at' =>'创建于','lastvisit_at' =>'最后一次访问','超级用户' =>'超级用户','状态' =>'地位','盐' =>'盐','requires_new_password' =>'需要新密码','login_attempts' =>'登录尝试','登录时间' =>'登录时间','login_ip' =>'登录IP','activation_key' =>'激活码','validation_key' =>'验证密钥','创建时间' =>'创造时间','更新时间' =>'更新时间','reset_token' =>'重置令牌','图像' =>'图像','地址' =>'地址',);}/*** 根据当前搜索/过滤条件检索模型列表.* @return CActiveDataProvider 可以根据搜索/过滤条件返回模型的数据提供者.*/公共功能搜索(){//警告:请修改以下代码以删除属性//不应该被搜索.$criteria=新的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);返回新的 CActiveDataProvider($this, array('标准'=>$标准,));}}

这是用户加入事件模型

45),//以下规则由 search() 使用.//请删除那些不应搜索的属性.数组('id, user_id, event_id, date_created, date_modified', 'safe', 'on'=>'search'),//http://stackoverflow.com/questions/18434575/to-store-datetime-automatically-using-yii);}/*** @return 数组关系规则.*/公共函数关系(){//注意:您可能需要调整关系名称和相关的//下面自动生成的关系的类名.返回数组('事件' =>数组(自我:: BELONGS_TO,'事件','event_id'),'用户' =>数组(自我:: BELONGS_TO,'用户','user_id'),);}/*** @return 数组自定义属性标签(name=>label)*/公共函数attributeLabels(){返回数组('id' =>'ID','user_id' =>'用户','event_id' =>'事件','date_created' =>'创建日期','date_modified' =>'修改日期',);}/*** 根据当前搜索/过滤条件检索模型列表.* @return CActiveDataProvider 可以根据搜索/过滤条件返回模型的数据提供者.*/公共功能搜索(){//警告:请修改以下代码以删除属性//不应该被搜索.$criteria=新的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);返回新的 CActiveDataProvider($this, array('标准'=>$标准,));}}

谢谢.

解决方案

 这是一个简单的解决方案,不是一个好的实践,但真的很简单您在 show loop 中解码您需要的值.. 对于快速解决方案可能很有用还有一个 allstat.php(在 UserJoinEvent 的视图文件中),它是<div><?phpforeach($user 作为 $show){$model = User::model()->findByAttributes(array('id'=>$show->user_id,));如果 (isset($model)) 然后 {回声'<h3>'.$show->user_id .' - ' .$model->用户名.'</h3>';} 别的 {回声'<h3>'.$show->user_id .' - 未找到用户名</h3>';}?>

I am using the following code to get the 5 most active user 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));
            } 

and the view file is

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

How can i get the names of the users againt their id, the name is from the User model. This is the code for the user model

<?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,
        ));
    }
}

and this is the user join event model

<?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,
        ));
    }
}

Thankyou.

解决方案

    This is a simple solution, not a good pratice but really simple 
    You decode the value you need inside the show loop.. For a fast solution can be useful 

And a allstat.php(in the view file of UserJoinEvent) which is

<div>
    <?php
        foreach($user as $show)
        {
           $model = User::model()->findByAttributes(array('id'=>$show->user_id,));
           if (isset($model)) then {
               echo '<h3>' . $show->user_id .  ' - ' . $model->username . '</h3>';
           } else {
               echo '<h3>' . $show->user_id .  ' - Username not found</h3>';
           }

        ?>
    </div>

这篇关于根据用户的 id 获取用户的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆