Yii2中的ModelSearch查询错误

时间:2015-10-22 08:06:36

标签: php mysql yii2

我有两张表projectworkload。我希望在Workload中添加一些项目中的show user,以及该项目中的其他成员(我也称为该项目的Team Member)。

我的想法是加入2个表projectworkload user_id条件,以便用户的项目有user_id,然后从中加入workload再次从用户项目中获取数据,表示团队成员的user_id和user_id将具有该项目。

我的代码在WorkloadSearch.php

public function searchWorkloadofUser($params) {
    $user_id = Yii::$app->user->id;
    $query = Workload::find()
        ->select(['workload.project_id', 'workload.commit_time', 'project.project_name', 'workload.from_date', 'workload.to_date', 'workload.workload_type', 'workload.comment'])
        ->join('INNER JOIN', 'project', 'workload.project_id=project.id')
        ->where('workload.user_id = '.$user_id)->orderBy('project.project_name ASC')->distinct();
    $query->join('INNER JOIN','workload', 'project.id = workload.project_id')->distinct();
}

我不明白为什么会出现错误:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'workload'
The SQL being executed was: SELECT COUNT(*) FROM (SELECT DISTINCT `workload`.`project_id`, `workload`.`commit_time`, `project`.`project_name`, `workload`.`from_date`, `workload`.`to_date`, `workload`.`workload_type`, `workload`.`comment` FROM `workload` INNER JOIN `project` ON workload.project_id=project.id INNER JOIN `workload` ON project.id = workload.project_id WHERE workload.user_id = 20) `c`

2 个答案:

答案 0 :(得分:2)

您需要在第二次加入时为用户表提供别​​名

e.g。

SELECT workload.project_id, workload.commit_time, p.project_name, ...
FROM workload 
LEFT JOIN project p ON p.id = workload.project_id 
WHERE workload.user_id = '1'

您的解决方案应该是(未经测试):

public function searchWorkloadofUser($params)
{
    $user_id = Yii::$app->user->id;
    $query = Workload::find()
        ->select(['workload.project_id', 'workload.commit_time', 'p.project_name', 'workload.from_date', 'workload.to_date', 'workload.workload_type', 'workload.comment'])
        ->join('INNER JOIN', 'project p'], 'workload.project_id=p.id')
        ->where('workload.user_id = '.$user_id)->orderBy('p.project_name ASC')->distinct();
    ...

答案 1 :(得分:2)

像其他人说的那样:你需要一个别名。您的错误消息告诉您:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'workload'

这是因为您将表格worload加入了workload表:

$query = Workload::find() //your `workload` table
    ->select([
        ....
    ])
    ->join('INNER JOIN', 'project p'], 'workload.project_id=p.id')
    ->where('workload.user_id = '.$user_id)
    ->orderBy('p.project_name ASC')
    ->distinct();

$query->join('INNER JOIN','workload', 'project.id = workload.project_id') //join the `workload` table to the `workload` table
    ->distinct();

如果要将表连接到自身,则必须至少为连接表定义别名。

我建议你引用yii2指南here concat字符串中描述的表名和列,但绑定params,如here和{{ {3}}。此代码应该适合您(未经测试):

$query = Workload::find() //your `workload` table
    ->select([
        '{{workload}}.[[project_id]]', //quoting tablenames and columns
        '{{%workload}}.[[commit_time]]', //add '%' when you're using table prefix
        ...
    ])
    ->join('INNER JOIN', 'project p'], 'workload.project_id=p.id')
    ->where('workload.user_id' = :user_id, [':user_id' => $user_id]) //You should bind params when use string format
    //or use hash format
    //->where(['workload.user_id' => $user_id])
    ...

$query->join('INNER JOIN','workload w2', 'project.id = w2.project_id') //Use the alias `w2` to  join the `workload` table to the `workload` table
    ->distinct();