Ожидайте, что orWhere () будет работать с andWhere (), а не где () - PullRequest
1 голос
/ 19 января 2020

У меня есть запрос:

topics = await topicRepository.createQueryBuilder('topic')
                               .leftJoinAndSelect('topic.user', 'user', 'topic.userId = user.id')
                               .where('topic.categoryId = :id', {
                                       id: categoryId,
                                     })
                               .andWhere('topic.title like :search', { search: `%${searchKey}%`})
                               // It should take the first where
                               .orWhere('user.pseudo like :search', { search: `%${searchKey}%` })
                               .addOrderBy(filter === 'latest' ? 'topic.created_at' : 'topic.repliesCount', 'DESC')
                               .take(limit)
                               .skip(skip)
                               .getMany();

Сгенерированный SQL запрос:

ВЫБЕРИТЕ DISTINCT distinctAlias. topic_id как \ "ids_topic_id \", distinctAlias. topic_created_at ИЗ (ВЫБРАТЬ topic. id AS topic_id, topic. title AS topic_title, topic. content AS topic_content, topic. created_at AS topic_created_at, topic. views AS topic_views, topic. repliesCount AS topic_repliesCount, topic. categoryId AS topic_categoryId, topic. userId AS topic_userId, topic. surveyId AS topic_surveyId, user. id AS user_id, user. email AS user_email, user. pseudo AS user_pseudo, user. password AS user_password, user. rank AS user_rank, user. avatar AS user_avatar, user. createdAt AS user_createdAt , user. lastActivity AS user_lastActivity, user. signature AS user_signature, user. post_count AS user_post_count, user. updatedAt AS user_updatedAt ОТ topic topic LEFT JOIN user user ON user. id = topic. userId AND (topi c .userId = user. id) ГДЕ topi c .categoryId = '5' AND topic. title как "% admin%" ИЛИ topi c .user.pseudo как "% admin%") distinctAlias ORDER BY distinctAlias. topic_created_at DES C, topic_id AS C LIMIT 20

Проблема здесь:

ГДЕ topi c .categoryId = '5' И topi c. Заголовок похож на '% admin%' ИЛИ ​​topi c .user.pseudo похож на '% admin%')

Я ожидал:

ГДЕ (topi c .categoryId = '5' И topi c .title вроде '% admin%') ИЛИ (topi c .categoryId = '5' И topi c .user.pseudo как '% admin%')

Я хочу, чтобы .илиWhere были ИЛИ из .andWhere вместо .where

Я не нашел никакой документации / проблемы по этому варианту использования.

1 Ответ

1 голос
/ 19 января 2020

Приоритет условий запроса можно контролировать с помощью класса Brackets :

topics = await topicRepository.createQueryBuilder('topic')
    .leftJoinAndSelect('topic.user', 'user', 'topic.userId = user.id')
    .where('topic.categoryId = :id', {
      id: categoryId,
    })
    .andWhere(new Brackets(qb => {
      qb.where('topic.title like :search', { search: `%${searchKey}%`})
      orWhere('user.pseudo like :search', { search: `%${searchKey}%` });                                 
    }))
    .addOrderBy(filter === 'latest' ? 'topic.created_at' : 'topic.repliesCount', 'DESC')
    .take(limit)
    .skip(skip)
    .getMany();

...