使用关系

时间:2016-07-22 08:40:17

标签: php sql yii2

我想获取用户及其角色的数据,但它返回错误:

  

SQLSTATE [42S22]:找不到列:1054未知列' auth_item.user_id'在' on条款'         正在执行的SQL是:SELECT COUNT(*)FROM`tblusers` LEFT JOIN`auth_item`ON`tblusers``id` =`auth_item``user_id`

我试过了: 搜索模型

$query = User::find()->joinWith('role');

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['id'=>SORT_ASC]],
        'pagination' => ['pageSize' => $pageSize]
    ]);

    return $dataProvider;

用户模型的关系

public function getRole()
{
    // User has_one Role via Role.user_id -> id
    return $this->hasOne(Role::className(), ['user_id' => 'id']);
}

角色模型的关系

public function getUser()
{
     return $this->hasMany(User::className(), ['id' => 'user_id']);
}

这是完整搜索:

public function search($params, $pageSize = 10)
{
   $query = User::find()->joinWith('role');

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['id'=>SORT_ASC]],
        'pagination' => ['pageSize' => $pageSize]
    ]);

    return $dataProvider;
    die();

    // if user is not 'theCreator' ( You ), do not show him users with this role
     // if user is not 'theCreator' ( You ), do not show him users with this role
    if (Yii::$app->user->can('theCreator')) {
        $query->where(['!=', 'item_name', 'theCreator']);
    }

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort'=> ['defaultOrder' => ['id'=>SORT_ASC]],
        'pagination' => ['pageSize' => $pageSize]
    ]);

    // make item_name (Role) sortable
    $dataProvider->sort->attributes['item_name'] = [
        'asc' => ['item_name' => SORT_ASC],
        'desc' => ['item_name' => SORT_DESC],
    ];

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        'id' => $this->id,
        'status' => $this->status,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ]);

    $query->andFilterWhere(['like', 'username', $this->username])
          ->andFilterWhere(['like', 'email', $this->email])
          ->andFilterWhere(['like', 'item_name', $this->item_name]);

    return $dataProvider;
}

这是authitem模型

<?php 
  namespace app\rbac;

  use yii\db\ActiveRecord;
  use Yii;

    class AuthItem extends ActiveRecord
    {  public static function tableName()
   { 
    return '{{%auth_item}}';
   }
   public static function getRoles()
   {
    if (Yii::$app->user->can('theCreator')) 
    {
        return static::find()->select('name')->where(['type' => 1])->all();  
    }
    else
    {
        return static::find()->select('name')
                             ->where(['type' => 1])
                             ->andWhere(['!=', 'name', 'theCreator'])
                             ->all();
    }
  }        
  }

这是代码更新: 在这一行

$query = User::find()->joinWith('role'); 

它实际上与auth赋值有关,如下所示:

<?php
namespace app\rbac;

  use app\models\User;
     use yii\db\ActiveRecord;
    use Yii;
     class Role extends ActiveRecord
   {
  public static function tableName()
   {
    return '{{%auth_assignment}}';
   }
   public function rules()
        {
    return [
        [['item_name'], 'required'],
        [['item_name'], 'string', 'max' => 64],
    ];
}

 public function attributeLabels()
{
    return [
        'item_name' => Yii::t('app', 'Role'),
    ];
}


public function getUser()
{
    // Role has_many User via User.id -> user_id
    return $this->hasMany(User::className(), ['id' => 'user_id']);
}


}

为什么会返回该错误?

1 个答案:

答案 0 :(得分:2)

如果您使用的是yii2中的默认rbac模型/模块,则可能是您关联了错误的模型/表..因为auth_itme模型不包含user_id列

/**
* This is the model class for table "auth_item".
*
* @property string $name
* @property integer $type
* @property string $description
* @property string $rule_name
* @property string $data
* @property integer $created_at
* @property integer $updated_at

可能需要关联auth_assignment表,其中包含auth_item与用户之间的关系

/**
 * This is the model class for table "auth_assignment".
 *
 * @property string $item_name
 * @property string $user_id
 * @property integer $created_at
 *
相关问题