简体   繁体   中英

Left join in select query Yii2 - Unknown column 'single' in 'on clause'

Why this query is not working?

public function actionInsert()
{
    $model = new NotificationsEvents();

    $date_at = (new Query())
        ->select(['single-events.date_at'])
        ->from('single-events')
        ->leftJoin('user', 'user.birthday = single-events.date_at');


    $event_id = (new Query())
        ->select(['single-events.id'])
        ->from('single-events')
        ->leftJoin('user', 'user.id = single-events.id');

    (new Query())->createCommand()->insert('notifications_events', [
        'type' => 7,
        'date_at' => $date_at,
        'event_id' => $event_id,
    ])->execute();

}

I need to insert user birthday in notifications_events.date_at , and user ID in notifications_events.event_id , but this code is not working:

Unknown column 'single' in 'on clause'

single-events is not a valid/safe name for table, because it contains - . You should either quote it in every possible place:

public function actionInsert() {
    $model = new NotificationsEvents();

    $date_at = (new Query())
        ->select(['{{single-events}}.date_at'])
        ->from('single-events')
        ->leftJoin('user', 'user.birthday = {{single-events}}.date_at');


    $event_id = (new Query())
        ->select(['{{single-events}}.id'])
        ->from('single-events')
        ->leftJoin('user', 'user.id = {{single-events}}.id');

    (new Query())->createCommand()->insert('notifications_events', [
        'type' => 7,
        'date_at' => $date_at,
        'event_id' => $event_id,
    ])->execute();
}

Or use some more practical name for table, like single_events .

另一种引用方法:使用背景:

`single-events`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM