简体   繁体   中英

Searching through children of an domain object in Grails GORM

How can I write this code correctly in Groovy / Gorm?

I have a PageComponent domain class that has many Content 's. I want to see if a particular PageComponent contains a Content with a specfific key .

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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