我有两个模型,它们有一对多的关系。这是我的课程。
预订
final class Booking: PostgreSQLModel {
/// The unique identifier for this `Todo`.
var id: Int?
/// A title describing what this `Todo` entails.
var user_id: User.ID
var product_id: Product.ID
var count: Int
/// Creates a new `Todo`.
init(id: Int? = nil, user_id: User.ID, product_id: Product.ID, count: Int) {
self.id = id
self.user_id = user_id
self.product_id = product_id
self.count = count
}
}
extension Booking {
var user: Parent<Booking, User> {
return parent(\.user_id)
}
var product: Parent<Booking, Product> {
return parent(\.product_id)
}
}
产品
final class Product: PostgreSQLModel {
/// The unique identifier for this `Todo`.
var id: Int?
/// A title describing what this `Todo` entails.
var name: String
var image_url: String
var description: String
var price: Int?
/// Creates a new `Todo`.
init(id: Int? = nil, name: String, image_url: String, description: String, price: Int) {
self.id = id
self.name = name
self.image_url = image_url
self.description = description
self.price = price
}
}
extension Product {
var bookings: Children<Product, Booking> {
return children(\.product_id)
}
}
现在,我想获取用户的所有预订,并且每次预订时我也要获取产品信息。因此,为此,我尝试连接Booking
和Product
表,但这会引发异常。
致命错误:“尝试!”表达式意外引发错误:⚠️CoreError:
Parent<Booking, Product>
不符合ReflectionDecodable
。 -id:CoreError.ReflectionDecodable :文件/BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang_Fall2018/swiftlang_Fall2018-1000.11.42/src/swift/stdlib/public/core/ErrorType.swift,第184行 程序以退出代码结束:9
这是我的加入代码。
let booking = Booking.query(on: req).join(\Product.bookings, to:\Booking.product).filter(\.user_id == userID).decode(BookingM.self).all()
答案 0 :(得分:0)
首先,使用您所拥有的查询,您无需加入- (void) zoomPinchGestureRecognizerAction: (UIPinchGestureRecognizer *) sender {
static CGFloat initialVideoZoomFactor = 0;
NSLog(@"Pinch Zoom Activated!");
// Find the current capture device.
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithDeviceType: AVCaptureDeviceTypeBuiltInWideAngleCamera
mediaType: AVMediaTypeVideo
position: self.defaultAVCaptureDevicePosition];
if (sender.state == UIGestureRecognizerStateBegan) {
initialVideoZoomFactor = captureDevice.videoZoomFactor;
} else {
CGFloat scale = MIN(MAX(1, initialVideoZoomFactor * sender.scale), 4);
[CATransaction begin];
[CATransaction setAnimationDuration: 0.01];
customPreviewLayer.affineTransform = CGAffineTransformMakeScale(scale, scale);
[CATransaction commit];
if ([captureDevice lockForConfiguration: nil] == YES) {
captureDevice.videoZoomFactor = scale;
[captureDevice unlockForConfiguration];
}
}
}
表,因为您无需对其进行查询或解码。
您的Product
语句:
join
不正确。您应该加入.join(\Product.bookings, to:\Booking.product)
属性,而不是加入Product.bookings
属性,因为产品的ID是Product.id
属性包含的内容。
因此,您的Fluent查询应如下所示:
Booking.product
我删除了let booking = Booking.query(on: req).join(\Booking.product, to:\Product.id).filter(\.user_id == userID).all()
调用,因为查询也已经.decoding
解码了查询结果。
要使用您的Booking
属性,您的查询应如下所示:
bookings