I'm trying to build a grails (2.1.0) application on top of a legacy database. It has a ton of tables, and I'd like very much to use only dynamic scaffolding. The issue is that some of the tables have a string as primary key, but template code in src/templates/scaffolding/Controller.groovy for e.g. show is
def show(Long id) {
def ${propertyName} = ${className}.get(id)
if (!${propertyName}) {
flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
redirect(action: "list")
return
}
[${propertyName}: ${propertyName}]
}
For the string keys, this seems to turn the string into null, and the get fails with the error $Domain not found with id null
.
If I run a generate-controller and change the signature to def show(String id)
, it works as expected.
So, is there a way to inspect the domain class at "dynamic scaffolding time" and write the method accordingly?
Within the controller template you have a domainClass
variable that gives you access to the GrailsDomainClass
representing the class for which you are generating the controller, so you can do something like this (and likewise for edit
, update
and delete
):
def show(${domainClass.identifier.type.name} id) {
which should generate def show(java.lang.Long id)
or def show(java.lang.String id)
as appropriate.