简体   繁体   English

Swift Vapor Fluent更新现有行

[英]Swift Vapor Fluent updating an existing row

I know you can do Model.delete() and Model.save() 我知道你可以做Model.delete() and Model.save()

Is there a way to update an existing row? 有没有办法更新现有行?

In Fluent 2.0 there is a property added to every Entity/Model: 在Fluent 2.0中,向每个实体/模型添加了一个属性:

let storage = Storage()

This object is holding additional information for each entity, if storage.exists is true it will update data when you call save() instead of trying to insert new item to database. 该对象为每个实体保存其他信息,如果storage.existstrue则它将在您调用save()时更新数据,而不是尝试将新项插入数据库。

Reading Fluent's code, I believe it saves the fields that are "dirty". 相信阅读Fluent的代码,我相信它可以保存“脏”的字段。 So, you would have to do something like this: 因此,您将必须执行以下操作:

let user = try User.find(42)
user.name = "Other Name"
try user.save()

I didn't try this, but I'll do it soon. 我没有尝试过,但是我会尽快做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM