Convert from query to ModelSearch of Yii2

北战南征 提交于 2019-12-23 17:18:47

问题


I'm new in Yii2, and I have a query with right result:

SELECT DISTINCT workloadTeam.project_id, wp.project_name, workloadTeam.user_id, workloadTeam.commit_time, wp.workload_type FROM 
(SELECT p.id, p.project_name, w.user_id, w.commit_time, w.comment, w.workload_type
     FROM workload as w, project as p
     WHERE w.user_id = 23 AND p.id = w.project_id) wp
INNER JOIN workload as workloadTeam ON wp.id = workloadTeam.project_id

But in my ModelSearch.php, I wrote:

$user_id = Yii::$app->user->id;

$subquery = Workload::find()->select('p.id', 'p.project_name', 'w.user_id', 'w.commit_time', 'w.comment', 'w.workload_type')
        ->from(['project as p', 'workload as w'])
        ->where(['user_id' => $user_id, 'p.id' => 'w.project_id']);

$query = Workload::find()
        ->select(['workloadTeam.project_id', 'wp.project_name', 'workloadTeam.user_id', 'workloadTeam.from_date', 'workloadTeam.to_date', 'workloadTeam.workload_type', 'workloadTeam.comment'])
        ->where(['', '', $subquery]);

$query->join('INNER JOIN', 'workload as workloadTeam', 'wp.id = workloadTeam.project_id');

It happended error:

SELECT COUNT(*) FROM `workload` INNER JOIN `workload` `workloadTeam` ON wp.id = workloadTeam.project_id WHERE `` (SELECT p.project_name `p`.`id` FROM `project` `p`, `workload` `w` WHERE (`user_id`=20) AND (`p`.`id`='w.project_id'))

And I can't fix it with right query above. You have any solution about this?


回答1:


Is this error shown in the Yii-debug toolbar? Then your query (which you mentioned as error) is probably only the count from the query which is listed before.

You missed to add the sub-query in from clause like you shown in your working sql. Add this in your where clause were just the wrong place. Put sub-queries in whereconditions, if you have scalar results, because you have to use this result with operands like =, >=, in...

This could work:

$user_id = Yii::$app->user->id;

$subquery = Workload::find()->select([
    'p.id as id',
    'p.project_name as project_name',
    'w.user_id as user_id',
    'w.commit_time as commit_time',
    'w.comment as comment',
    'w.workload_type as workload_type'
])
->from([
    'project as p',
    'workload as w'
])
->where([
    'user_id' => $user_id,
    'p.id' => 'w.project_id'
]);

$query = Workload::find()
    ->select([
        'workloadTeam.project_id',
        'wp.project_name',
        'workloadTeam.user_id',
        'workloadTeam.from_date',
        'workloadTeam.to_date',
        'workloadTeam.workload_type',
        'workloadTeam.comment'
    ])
    ->from([$subquery => 'wp']); //you were missing this line

$query->join('INNER JOIN', 'workload as workloadTeam', 'wp.id = workloadTeam.project_id');

But you don't use any selects from your workload table in your main-query $query...

Since I don't know what's your goal to achieve I can't help you at this topic...



来源:https://stackoverflow.com/questions/33278830/convert-from-query-to-modelsearch-of-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!