簡體   English   中英

我如何在 adonis 中使用 Lucid 模型/查詢生成器進行此查詢?

[英]How i can make this query using lucid models/query builder in adonis?

我需要進行一個查詢,返回一本書的所有 book_unit_questions。

所以,我有 Book.Id。

我正在嘗試類似的東西:

SELECT BO.id,
BUQ.description
FROM book_unit_question BUQ 
JOIN book_unit BU
ON(BUQ.book_unit_id = BU.book_id)
INNER JOIN Books BO
ON(BU.book_id = 1)

但是這種方式是從其他書中返回 id,而我預計會有一些 id 1:

在此處輸入圖像描述

我的遷移文件:

class BookSchema extends Schema {
  up () {
    this.create('books', (table) => {
      table.increments()
      table.string('code').notNullable().unique()
      table.string('description')
      table.string('authors')
      table.boolean('status').defaultTo(false)
      table.integer('user_id').references('id').inTable('users')
      table.timestamps()
    })
  }

class BookUnitSchema extends Schema {
  up () {
    this.create('book_unit', (table) => {
      table.increments()
      table.integer('book_id').references('id').inTable('books').notNullable()
      table.integer('unit').notNullable()
      table.integer('sequence').notNullable()
      table.string('description')
      table.integer('qt_question')
      table.boolean('status').defaultTo(false)
      table.integer('user_id').references('id').inTable('users')
      table.timestamps()
      table.unique(['unit', 'sequence', 'book_id'])
    })
  }

class BookUnitQuestionSchema extends Schema {
  up () {
    this.create('book_unit_question', (table) => {
      table.increments()
      table.integer('book_unit_id').references('id').inTable('book_unit')
      table.string('question_form')
      table.string('option_form')
      table.string('type_answer')
      table.string('description')
      table.string('correct_answer_description')
      table.integer('correct_answer_description_id')
      table.text('image_sound')
      table.boolean('status').defaultTo(false)
      table.integer('user_id').references('id').inTable('users')
      table.timestamps()
    })
  }

編輯:此版本不使用manyThrough()

Controller:

    const BookUnit = use('App/Models/BookUnit')
    const Question = use('App/Models/BookUnitQuestion')

    const bIds = await BookUnit.query().where('book_id', 1).ids() //Get all bookUnit ids

    const questions = await Question.query().whereIn('book_unit_id', bIds).fetch()

    return questions

分頁:

const questions = await Question.query().whereIn('book_unit_id', bIds).paginate(2, 2)

遷移文件:

this.create('books', (table) => {
      table.increments()
      table.string('code').notNullable()
      table.string('description').notNullable()
      table.timestamps()
})

this.create('book_units', (table) => {
      table.increments()
      table.integer('book_id').unsigned().references('id').inTable('books')
      table.integer('unit')
      table.timestamps()
})

this.create('book_unit_questions', (table) => {
      table.increments()
      table.integer('book_unit_id').unsigned().references('id').inTable('book_units')
      table.integer('description')
      table.timestamps()
})

如果您有任何問題,請不要猶豫

經過一些試探,這段代碼適用於我的目的:

 async show(request){
        const bookQuestions = await BookUnitQuestion
                                    .query()
                                    .with('book_unit')
                                    .with('user')
                                    .with('book', (builder) => {
                                        builder.where('id', request.params.id)
                                    })
                                    .paginate()

    return bookQuestions

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM