简体   繁体   English

在Grails GORM中搜索域对象的子级

[英]Searching through children of an domain object in Grails GORM

How can I write this code correctly in Groovy / Gorm? 如何在Groovy / Gorm中正确编写此代码?

I have a PageComponent domain class that has many Content 's. 我有一个包含许多ContentPageComponent域类。 I want to see if a particular PageComponent contains a Content with a specfific key . 我想看看特定的PageComponent是否包含带有特定Content

I thought I could say: 我以为我可以说:

def pageComponent = PageComponent.get(1);

if (pageComponent.contents.findByKey("textnode") {
  // update
} else {
  // insert
}

At the moment, I'm using this instead. 目前,我正在使用它。 Not very elegant... 不太优雅...

def pageComponent = PageComponent.get(1);

def content = Content.withCriteria {
    eq "pageComponent.id", pageComponent.id
    eq "key", "textnode"
}

您还可以使用动态查找器:

Content.findByPageComponentAndKey(pageComponent, "textnode")

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

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