我可以在与用户表没有关系的表中获取用户名吗



我正在尝试获取business表视图中的username,即userbusiness.phpBusiness表与user表没有关系,但与reviewbusiness表有一对多关系。此外,Reviewbusiness与用户表具有一对多关系。Review business具有以下属性:user_id、business_id、rating、review。我的问题是,如何在business视图(userbusiness.php)中获得与user表没有关系的username

这是我的商业模式

    <?php
    /**
     * This is the model class for table "business".
     *
     * The followings are the available columns in table 'business':
     * @property integer $id
     * @property string $business_name
     * @property string $image
     * @property string $business_description
     * @property string $opening_hours
     * @property string $closing_hours
     * @property string $days
     * @property string $Holiday
     *
     * The followings are the available model relations:
     * @property Address[] $addresses
     * @property BusinessItems[] $businessItems
     * @property BusinessPackage[] $businessPackages
     * @property Facilities[] $facilities
     * @property ReviewBusiness[] $reviewBusinesses
     * @property SubCategoryBusiness[] $subCategoryBusinesses
     */
    class Business extends CActiveRecord
    {
        /**
         * @return string the associated database table name
         */
        public function tableName()
        {
            return 'business';
        }
        /**
         * @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('business_name, business_description, opening_hours, closing_hours, days', 'required'),
                array('business_name', 'length', 'max'=>60),
                array('image, opening_hours, closing_hours, days, Holiday', 'length', 'max'=>45),
                array('business_description', 'length', 'max'=>500),
                // The following rule is used by search().
                // @todo Please remove those attributes that should not be searched.
                array('id, business_name, image, business_description, opening_hours, closing_hours, days, Holiday', 'safe', 'on'=>'search'),
             array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'safe' => false,'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(
                'addresses' => array(self::HAS_MANY, 'Address', 'business_id'),
                'businessItems' => array(self::HAS_MANY, 'BusinessItems', 'business_id'),
                'businessPackages' => array(self::HAS_MANY, 'BusinessPackage', 'business_id'),
                'facilities' => array(self::HAS_MANY, 'Facilities', 'business_id'),
                'reviewBusinesses' => array(self::HAS_MANY, 'ReviewBusiness', 'business_id'),
                'subCategoryBusinesses' => array(self::HAS_MANY, 'SubCategoryBusiness', 'business_id'),
            );
        }
        /**
         * @return array customized attribute labels (name=>label)
         */
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'business_name' => 'Business Name',
                'image' => 'Image',
                'business_description' => 'Business Description',
                'opening_hours' => 'Opening Hours',
                'closing_hours' => 'Closing Hours',
                'days' => 'Days',
                'Holiday' => 'Holiday',
            );
        }
        /**
         * Retrieves a list of models based on the current search/filter conditions.
         *
         * Typical usecase:
         * - Initialize the model fields with values from filter form.
         * - Execute this method to get CActiveDataProvider instance which will filter
         * models according to data in model fields.
         * - Pass data provider to CGridView, CListView or any similar widget.
         *
         * @return CActiveDataProvider the data provider that can return the models
         * based on the search/filter conditions.
         */
        public function search()
        {
            // @todo Please modify the following code to remove attributes that should not be searched.
            $criteria=new CDbCriteria;
            $criteria->compare('id',$this->id);
            $criteria->compare('business_name',$this->business_name,true);
            $criteria->compare('image',$this->image,true);
            $criteria->compare('business_description',$this->business_description,true);
            $criteria->compare('opening_hours',$this->opening_hours,true);
            $criteria->compare('closing_hours',$this->closing_hours,true);
            $criteria->compare('days',$this->days,true);
            $criteria->compare('Holiday',$this->Holiday,true);
            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));
        }
        /**
         * Returns the static model of the specified AR class.
         * Please note that you should have this exact method in all your CActiveRecord descendants!
         * @param string $className active record class name.
         * @return Business the static model class
         */
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }
    }

这是我对商业模式的评价

class ReviewBusiness extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'review_business';
    }
    /**
     * @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, business_id, review, rating', 'required'),
            array('user_id, business_id, rating', 'numerical', 'integerOnly'=>true),
            array('review', 'length', 'max'=>500),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, user_id, business_id, review, rating', '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(
            'business' => array(self::BELONGS_TO, 'Business', 'business_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',
            'business_id' => 'Business',
            'review' => 'Review',
            'rating' => 'Rating',
        );
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo 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('business_id',$this->business_id);
        $criteria->compare('review',$this->review,true);
        $criteria->compare('rating',$this->rating);
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return ReviewBusiness the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

最后是我的用户模型

class User extends CActiveRecord
{
    /**
     * @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', 'length', 'max'=>45),
            array('password, email, activkey', 'length', 'max'=>120),
            array('salt, validation_key', 'length', 'max'=>255),
            array('activation_key', 'length', 'max'=>128),
            array('reset_token', 'length', 'max'=>250),
            array('profilepic', 'length', 'max'=>450),
            array('create_at, lastvisit_at, create_time, update_time', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, username, password, email, 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, profilepic', '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(
            'itemReviews' => array(self::HAS_MANY, 'ItemReview', 'user_id'),
            'profiles' => array(self::HAS_ONE, 'Profiles', 'user_id'),
            'reviewBusinesses' => array(self::HAS_MANY, 'ReviewBusiness', 'user_id'),
        );
    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
            'email' => 'Email',
            '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',
            'profilepic' => 'Profilepic',
        );
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo 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('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('profilepic',$this->profilepic,true);
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

我认为你可以通过这种方式简单地获得用户名

 Yii::app()->user->name;

你也可以通过这种方式获得用户id

 Yii::app()->user->getId(); 

查看此文档表单了解更多信息http://www.yiiframework.com/doc/api/1.1/CWebUser/

为了检索给定userId的用户名,你可以这样做

$theUsername = User::model()->find('id=:id',array(':id'=>$yourid))->username;

相关内容

  • 没有找到相关文章

最新更新