0

我想获得类似以下结构的数据。

显示记录

// 像这样的媒体表::-

import UIKit

@objc(Media)

class Media: DBObject {    
    dynamic var media_id : NSNumber!;    
    dynamic var post_id : Post!;    
    dynamic var desc : String!;    
    dynamic var url : String!;
}

我想用“媒体”表中的媒体列表从“发布”表中获取数据。

一篇文章可能是多种媒体也可能不是(媒体列表为零)。

请建议 DBAccess Query 在 Swift 中获取 DBResultSet。

4

1 回答 1

1

您向查询添加了一个参数joinTo

这是头文件中的条目。

/**
 * Used to include "joined" data within the query string, you must use the tablename.columnname syntax within a where statement

 *
 * @param (Class)joinTo the class you would like to pwrform a SQL JOIN with
 * @param (NSString*)leftParameter the property name that you would like to use within the local object to match against the target
 * @param (NSString*)targetParameter the property within the class you wish to join with that will be matched with the left parameter.
 * @return (DBQuery*) this value can be discarded or used to nest queries together to form clear and concise statements.
 */
- (DBQuery*)joinTo:(Class)joinClass leftParameter:(NSString*)leftParameter targetParameter:(NSString*)targetParameter;

下面是一个快速链接两个表(员工和登录)的示例。

Employee.query().joinTo(Login, leftParameter: "login", targetParameter: "Id")

很明显,类在运行时是不能改变的,所以你在 Dictionary 对象中找到结果,joinedResults

尽管您所描述的更多地与关系有关,但我建议您在此处查看一对多示例:

在 DBAccess 中关联两个对象

因此,在您的特定情况下,您可以将以下方法添加到您的 Post 类中。

func media -> DBResultSet {
        return Media.query().whereWithFormat("post_id = %@", withParameters: [self.post_id]).fetch()
    }

这将从媒体表中查找并返回结果。

于 2016-05-11T11:49:12.733 回答