我有一个通过Attends表关联的Persons表和一个Events表,这样当我希望记录某人将以特定角色参加特定事件的事实时,记录存在于Attends表中(例如组织者等):
I have a Persons table and an Events table that are related via an Attends table, such that a record exists the Attends table when I wish to record the fact that a person is due to attend a particular Event in a particular role (e.g. organiser, etc.):
Persons +----+-------------+ | id | surname | +----+-------------+ | 1 | Robinson | | 2 | Savage | ... Events +----+-----------------------+ | id | name | +----+-----------------------+ | 1 | Half Term Competition | | 2 | Christmas Competition | ... attends +---------+----------+--------+ | eventId | personId | roleId | +---------+----------+--------+ | 1 | 1 | 1 | | 1 | 2 | 6 | ...我想产生一个事件中谁将去向以及他们在做什么的列表,但是我找不到在Vapor查询中使用.join()来做到这一点的方法.似乎可以对联接的表进行筛选,但不包括任何字段.我错过了什么吗?还是不可能?
I want to produce a list of who is going and what they are doing at an event, but I can't find a way of doing it using .join() in Vapor query. It seems that you can filter on the joined table, but not include any of the fields. Am I missing something or is this impossible?
我已经尝试过以下主题的多种变体:
I've tried many variations on the theme of:
Person.join(Attends.self).filter(Attends.self,Event.foreignIdKey == eventId)我获得了正确的人"记录,但是无法从加入的Attends记录中访问他们的角色".
I get the correct Person records, but no way of accessing what their 'role' is from the joined Attends record.
如果我执行原始查询,则它将返回Node表示形式.我可以使用Leaf轻松地查看它,但是如何以我想生成带有图像等的PDF的方式来对其进行迭代并不是很明显.
If I do a raw query, then it returns a Node representation. I can easily view this using Leaf, but it is not at all obvious how to iterate over it in the way I would like to generate a PDF with images, etc.
推荐答案流利的关系可能是您所需要的.
据我所知,您正在使用多对多"(同级)关系,因此此代码可能就是您想要的:
From what I can tell, you are using a Many to Many (sibling) relation, so this code might be what you want:
extension Persion { var roles: Siblings<Persion, Role, Pivot<Person, Role>> { return siblings() } }然后获得角色:
let roles = person.roles.all()由于看不到您的代码,我可能会错了,但它应该为您提供一些指针.
I might have got this wrong as I can't see your code, but it should give you some pointers.
更多推荐
是否可以在Vapor中访问联接表中的字段?
发布评论