簡體   English   中英

是否可以在Lucid的AdonisJs中執行嵌套查詢構建器?

[英]Is it possible to do a nested query builder in AdonisJs with Lucid?

我想知道是否可以在AdonisJs中使用Lucid進行嵌套的“ .scope”查詢? 我想獲取給定Office的OrderItems中所有不具有“ d”狀態的Order(Laravel為此使用whereHas('orders.items'))。

一個例子:

const office = yield Office
  .query()
  .with('orders.items')
  .where('id', officeId)
  .scope('orders.items', (builder) => {
    builder.whereNot('status','d')
  })
  .first()

但這只會過濾掉所有狀態為'd'的OrderItem,因此我仍然得到一個包含空Item數組的Order的列表:

office: {
  orders: [{
    items: [{
      status: 'p'
    }, {
      status: 'c'
    }]
  },{
    items: []
  },{
    items: []
  }]]
}

我想得到這個結果:

office: {
  orders: [{
    items: [{
      status: 'p'
    }, {
      status: 'c'
    }]
  }]]
}

您可以執行嵌套查詢,但它們不會過濾頂級行。 簡而言之,Lucid不支持whereHas

返回結果是lodash集合,因此您可以通過刪除空數組來過濾結果。 我建議在Github上為它創建一個問題。

更新

對相同的支持已添加。 檢出此線程https://github.com/adonisjs/adonis-lucid/issues/92

暫無
暫無

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

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