Reputation: 329
I have two domain classes :
class User {
Client client
String firstName
String lastName
String email
String password
String address
String city
String state
String zip
String phone
Integer active
Date changedate
static belongsTo = [client : Client]
static constraints = {
firstName(nullable: false, blank:false)
lastName(nullable: false, blank:false)
email(nullable: false, blank: false)
address(nullable:false,blank:false)
city(nullable:false,blank:false)
state(nullable:false,blank:false)
zip(nullable:false,blank:false)
phone(nullable:false,blank:false)
active(nullable:false,blank:false)
changedate(nullable:false,blank:false)
}
}
class Client {
String name
String webaddress
String address
String city
String state
String zip
String phone
Integer active
Date changedate
static constraints = {
name(unique:true, nullable: false, blank:false)
address(nullable:false,blank:false)
city(nullable:false,blank:false)
state(nullable:false,blank:false)
zip(nullable:false,blank:false)
phone(nullable:false,blank:false)
active(nullable:false,blank:false)
changedate(nullable:false,blank:false)
}
}
Now the two tables are generated and a foreign key exists from user to client. I cannot for the life of me despite reading the grails help docs figure out how to insert a user record with a corresponding client. This is the current code in my controller. The client inserts correctly but will not insert the user record.
def register(){
Client newClient = new Client (
name: params.Company,
webaddress: params.WebAddress,
address: params.CompanyAddress,
city: params.CompanyCity,
state: params.CompanyState,
zip: params.CompanyZip,
phone: params.CompanyPhone,
active: 1,
changedate: new Date()
)
newClient.save()
User user = new User (
client: newClient,
firstName: params.FirstName,
lastName: params.LastName,
email: params.userName,
password: params.Password,
address: params.Address,
city: params.City,
state: params.State,
zip: params.Zip,
phone: params.Phone,
active: 1,
changedate: new Date()
)
user.save()
}
Upvotes: 0
Views: 350
Reputation: 790
Try the following code it will point out where exactly the validation problem is while saving the object to db if any :
def register(){
Client newClient = new Client (
name: params.Company,
webaddress: params.WebAddress,
address: params.CompanyAddress,
city: params.CompanyCity,
state: params.CompanyState,
zip: params.CompanyZip,
phone: params.CompanyPhone,
active: 1,
changedate: new Date()
)
if(newClient.save(flush: true)){
User user = new User (
client: newClient,
firstName: params.FirstName,
lastName: params.LastName,
email: params.userName,
password: params.Password,
address: params.Address,
city: params.City,
state: params.State,
zip: params.Zip,
phone: params.Phone,
active: 1,
changedate: new Date()
)
if(!user.save(flush: true)){
user.errors.each {
println it
}
}
}
else {
newClient.errors.each {
println it
}
}
}
Upvotes: 3