[英]Chaining Multiple Query Filters in Vapor - Server Side Swift
我們可以使用以下方法在蒸氣中應用簡單的查詢過濾器:
// User is my model object connecting corresponding MySQL database table
let aUser = try User.query().filter("user_email", "asd@example.com")
我們如何像SQL查詢一樣將多個查詢過濾器與AND或OR條件鏈接在一起?
舉例來說,如果我們需要使用AND
條件將filter("user_email", "asd@example.com")
和filter("user_password", "123456")
結合起來,該如何實現?
據我所知.filter函數在檢查要過濾的條件后會拋出布爾值。 因此,您可以嘗試將兩個條件與邏輯AND運算符組合在一起。 對於適當的解決方案,最好了解對象User:的結構和屬性:但是建議是:
let aUser = User.query().filter { condition1 && condition2 }
如果兩個條件都為真,則過濾器只會將User.query()的一個元素傳遞給aUser。 確保User.query()是一個數組,並且在條件中使用$ 0引用當前對象。 過濾器函數還將返回一個數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.