Grails searchable relationship - grails

I am wondering how to return specific domain with searchable?
For example we have domain A and B. Both domain are searchable and have relationship: A has many B and B belongs to A. Another case A and B have many-to-many relationship.
Now when I search for item, I must always return A item. In my case let say I found matches in B, I need to return all As for each B. Other way around should work as well.
Currently I do a search query is searchable services:
def searchResults = searchableService.search(params.q, params)
Is there a way to get all related A domain for any search results?
Thank you.

Make both your domains(A & B) searchable and then add DomainObject component:true in searchable properties.
class A{
String name
static hasMany = [bclass: B]
static searchable = {
bclass component: true
}
}
class B{
String color
static belongsTo= [aclass: A]
static searchable = true
}
Suppose in B there are some rows with color field value as 'red'
A.search("red")
will return all the instances of A, which has its child class B with color field value as 'red'

Related

How can I get dateCreated in Has-Many Relation in Grails?

I have the following domain classes:
class A {
hasMany = [bs : B]
}
class B { }
Note that B has no backward relation to a. GORM creates a join table in my MYSQL database a_b. This table has two columns the id of a and the id of b.
How can I get a dateCreatedin the join table?
Create a model of the join table yourself and add whatever properties you want on it. Simple, and done.
For example:
class A {
static hasMany = [bs: JoinB]
}
class JoinB {
static belongsTo = [a: A]
B b
Date dateCreated
static mapping = {
autoTimestamp true // default, but I like to be explicit about it.
}
}
class B {
String whatever
}
(Careful of typos etc. I just did that off the top of my head)

Grails - get domain relationship id without fetching the whole object

I have, for example, the following domain objects:
class A {
B b
static constraints = {
b nullable: true
}
}
class B {
}
Given instance of A, I would like to fetch only the id of B.
I tried the following, but received null every time:
def id = a.bId
Is it possible to fetch the id of b without doing a.b.id ?
You will need to enhance your domain to use GORM based mapping hints (given to Hibernate) to accomplish this.
Your domain could look something like this:
class A {
static hasOne = [b: B]
}
class B {
// stuff
}
Using the hasOne will allow Hibernate to manage the association and thus allows you to use the a.bId notation.
Hope this helps.

Grails: Children of Children

I have a question about GORM and "multiple" hasmany relationships, and I didn't find an answer in my previous searches.
Let's say we have three domains:
class A {
...
static hasMany = [Bs: B]
}
and
class B {
...
static belongsTo = A
static hasMany = [Cs: C]
}
and
class C {
static belongsTo = B
String name
dateCreated date
}
I want to know if it is possible to get a list of objects of the class C, sorted by dateCreated, using an object of the class A (something like C.findAll(...., a: a.id) ) or if I have to use a more complex query ?
Best regards,
Its a little more difficult because you aren't storing a back reference to the parent objects
Something like this - I didn't test it myself
A.executeQuery("select distinct c from A a join a.bs as b join b.cs as c where a = :a", [a: a])
If B has:
static belongsTo = [a:A]
then you can do:
C.withCriteria {
a{
eq('id', <a's id here>)
}
order('dateCreated', 'desc')
}

Grails domain self-reference recursive call

Here is my domain class:
class Category {
String name
static hasMany = [subCategories: Category]
static belongsTo = [parentCategory: Category]
static mapping = {
subCategories joinTable: false, column: "parent_id"
}
static constraints = {
parentCategory nullable: true
}}
Category is a domain class and has self-reference to both parent and list of children.
Now I want something like this: given a parent category id, I want a list of all sub-categories belong to this id. (NB: not direct children, all the children under the id)
For example, id 1 has children 2 and 3, and 2 has children 4 and 5.
Given I got category id 1 from client, I want a sub categories with id 2,3,4,5
Taken advantage of Groovy, what is the best code to implement that?
Untested code but might get you moving in the right direction. There might be a "groovier" way to do this, but I'm not sure.
def findAllChildren(category, results = []) {
category.subCategories.each { child ->
results << child
findAllChildren(child, results)
}
}
def someOtherMethod() {
def allChildren = []
findAllChildren(parentCategory, allChildren)
}

Criteria on a one-to-many relation

My domain contains a one-to-many relationship like this:
class A {
B b
}
class B {
String name
}
I want to create a criteria query on A which will look for A to have the B object with the given name. It may return multiple entries. So... compare a given string with the "name" field from B and return the list of entries of type A for which B matches the name.
Thx!
Unless I am missing something that should be pretty easy:
def instanceList = A.withCriteria {
b {
eq('name','whatever')
}
}

Resources