Krishna
Krishna

Reputation: 27

How to save a Foreign key from a controller class in Grails?

Am a new comer to Groovy nd Grails.I dont know how to save a foreign key into a child table. I have two domain class called Person and Telephone.I hust tried to save but it doesn't work.please help me.

Person.groovy

class Person {

    String name
        String password        
    static constraints = {
        name(blank:false)
                password(blank:false)
    }
    static hasMany = [telephone:Telephone]
    @Override
    public String toString() {
        // TODO Auto-generated method stub
    name
    }
}

Telephone.groovy

class Telephone {

    String number
    Person person
    static constraints = {
        number(blank:false)
        person()
    }
    static belongsTo = [person:Person]
    @Override
    public String toString() {
        // TODO Auto-generated method stub
    number
    }
}

The loggin person Id stored into a session variable.

session.setAttribute("user_id")

Then I tried to save the Number.

TelephoneController.groovy

class TelephoneController {

     def index() {
        redirect(action:'create')
         }
    def create(){

    }
def save(){
        def number=new Telephone(params)
        int user_id=Integer.parseInt(session.getAttribute("user_id").toString())
                params.person=user_id       
        if(!number.hasErrors() && number.save()){
            println "save"
            flash.toUser="Person Details [${number.number}] has been added."
            redirect(view:'create')
        }
    }
}

But it doesn't work.please help me.

Upvotes: 1

Views: 1314

Answers (2)

topr
topr

Reputation: 4612

I agree with the 1st answer. You have overcomplicated the code. If you only want to store many telephone numbers for a Person, something like this should be enough:

class Person {

    String name
    String password

    static hasMany = [phoneNumbers:String]

    static constrains = ...
}

Basically you don't need another domain class to store only a list of numbers for the Person. Then in the controller you need to:

    def save() {
        int user_id = session['user_id'] // no need to parse, Groovy will handle
        def user = Person.get(user_id) // you need to fetch the Person from database (the same applies when using additional Telephone)
        user.phoneNumbers << params.newNumber // the << operator acts here like add(String) method
        user.save()
    }

If decided to using another Telephone class it would look like:

    def save() {
        int user_id = session['user_id']
        def user = Person.get(user_id)
        def number = new Telephone(params)
        number.person = user // important to create relation
        number.save() // pay attention here, that in previous example the Person was saved, but now the Telephone is
    }

Also you don't need to declare Person field at Telephone class when using static belongsTo = [person:Person]. The property will be created anyway.

Last tip, after creating your domain class(es) try to run command grails generate-all Person (and if still used grails generate-all Telephone). This will 'scaffold' default controllers and views for your domain classes and you can use them as an example.

Upvotes: 1

Tom Metz
Tom Metz

Reputation: 919

Your code looks a little bit overcomplicated, I believe, your goal should be achieved much easier, try to look into the Grails documentation, which describes the one-to-many relation, you are using: http://grails.org/doc/latest/guide/GORM.html#oneToMany

Upvotes: 0

Related Questions