views:

50

answers:

2

Hi, I have the following domains classes:

class Posts{
      String Name
      String Country
      static hasMany = [tags:Tags]

        static constraints = {
        }
    }


class Tags{

    String Name
    static belongsTo = Posts
    static hasMany = [posts:Posts]
    static constraints = {
    }
  String toString()
  {
      "${TypeName}"

  }
}

Grails creates an another table in the database i.e. Posts_Tags.
My requirement is:

E.g. 1 post has 3 tags. So, in the Posts_Tags table there are 3 rows.

How can I access the table Posts_Tags directly in my code so that I can manipulate the data or add some more fields to it.

A: 

Use the normal groovy sql API. For an example of how to get a groovy SQL object and execute sql queries see this

Jared
This doesn't answer the 2nd part of his question about how to add extra fields to the join table
Don
Yes it does, if you have a groovy SQL object you can use alter table statements, insert, etc.
Jared
+2  A: 

If you want to access the join table (Posts_Tags) directly, or add properties to it, then you must define it as a separate PostTag domain class. You then split your many-many relationship between Post and Tag into 2 one-to-many relationships (one from Post to PostTag and one from Tag to PostTag).

Here's a comprehensive example of how to perform the mapping and add properties to the join table - in this example Membership is the join table.

Don