Grails - 从另一个租户检索对象

Grails - Retrieve an object from another tenant

我有一个 grails 系统,已经 运行 在服务器上,我们使用租户解决方案来区分公司的分支机构,但现在我需要将信息从一个分支机构恢复到另一个分支机构。

重点是当我在我的模型中进行以下查询时:

def expedition = Expedition.findByCode(row.code)

如果我的探险是由一个分支机构发布的,那么这个发现对我来说 return 没有任何意义,毕竟这是分支机构不填写彼此信息的初始架构。

有人知道我该怎么做吗?它可能只是 return 那个时候的那个对象,或者模型中的一些注释,但我不想删除我的多租户结构,因为我仍然需要阻止一些信息。

您可以使用 grails.gorm.multitenancy.Tenants class 上的方法来实现:

允许跨任何租户查找:

def expedition = Tenants.withoutId { Expedition.findByCode(row.code) }                    

指定租户

Long otherTenantsId = 2L
def expedition = Tenants.withId(otherTenantsId) { Expedition.findByCode(row.code) }                    

当然,这样做或信任租户 ID 的用户输入时要小心,因为它会授予对其他用户数据的访问权限。

请注意,如果您想将其添加到 class/method 级别,还有 @WithoutTenant