是否可以在Vapor中访问联接表中的字段?

编程入门 行业动态 更新时间:2024-10-23 03:16:56
本文介绍了是否可以在Vapor中访问联接表中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个通过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中访问联接表中的字段?

本文发布于:2023-10-23 02:08:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   Vapor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!