Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know who is the parent in a Grails hasMany relationship?

We have an old application where the relationship is defined as below:

class Practice {
   String name
   static hasmany = [doctors:Doctor]
}

and

class Doctor {
  String name
}

There is not a belongsTo relationship defined in Doctor as we do not want to cascade the delete of a doctor when Practice is deleted. This is a very old code and do not want to change it.

Now according to the new functionality, the user should know which Practices the Doctor is linked to while viewing the details of a Doctor. Can anyone help me knowing which is the easiest way of achieving this without making changes to the domain object?

like image 514
Shantanu Wagh Avatar asked Mar 20 '12 16:03

Shantanu Wagh


1 Answers

If variable doctor contains the doctor you want to list practices from, you can get a list of Practice objects having this doctor in their doctors relationship by issuing the following criteria query:

def practices = Practice.withCriteria {
  doctors {
    idEq(doctor.id)
  }
}
like image 125
Antoine Avatar answered Nov 15 '22 03:11

Antoine