A
A
Artem Prokhorov2021-07-30 13:38:36
Yii
Artem Prokhorov, 2021-07-30 13:38:36

How to get data without connection?

return Project::find()
            ->leftJoin('project_user', 'project_user.project_id = project.id')
            ->select(['project.id', 'project.title'])
            ->where(['!=', 'project_user.user_id', $user_id])
            ->asArray()
            ->all();


I need to get all projects that do $user_idn't have a link in project_user. How to do it?
The problem with this query is that it will still return a project with which the user has no connection if the table project_userhas a connection with another user (that is, if there are 1 or more records with this project).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vilinyh, 2021-07-30
@kotcich

return Project::find()
    ->alias('p')
    ->leftJoin('project_user pu', 'pu.project_id = p.id and pu.user_id = :userId')
    ->select(['p.id', 'p.title'])
    ->where(['pu.user_id' => null])
    ->params(['userId' => $user_id])
    ->asArray()
    ->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question