|
I am trying to create the following view:
<g:each status="i" in="${roles}" var="role"> <div class="${(i%2)==0 ? "even":"odd"} row" id="${role.id}"> <div class="column">${role.name}</div> <div class="column">${role.crated_by}</div> <div class="column">${role.dateCreated}</div> <div class="column">${role.updated_by}</div> <div class="column">${role.lastUpdated}</div> </div> </g:each> the groovy object is defined as: import java.util.Date; class Role { String name Date dateCreated Date lastUpdated String created_by String updated_by static hasMany = [role_services: Role_Service] static mapping = { id generator:'sequence', params:[sequence:'ROLE_SEQ'] dateCreated column: 'CREATION_DATE' lastUpdated column: 'UPDATE_DATE' version false } static constraints = { } } the controller is defined as: class RoleController { def scaffold = Role def roleForm(){ def roles = [roles: Role.executeQuery("Select name, created_by, dateCreated, updated_by, lastUpdated from Role r")] render( view: "roleServicesForm", model: roles) } } I am getting the following error: Exception evaluating property 'id' for java.util.Arrays$ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: id for class: java.lang.String I would expect the roles variable to be defined as an array. However i would not expect any of the attributes Role to be defined as an array. the error occurs regardless of which attribute is used from the Role object. I have searched he form and Google without successes. I belive it is how i have my controller defined that might be giving me the problem. Thanks for any help you can give me. Stu |
|
Why are you using executeQuery()?
Why not just Role.list() which returns a list of Role objects. def roleForm(){
render( view: "roleServicesForm", model: [roles: Role.list()] ) } Also, your view has a typo in crated_by, which would fail too. -Aaron
On Fri, Jun 15, 2012 at 2:33 PM, stuart.simpson <[hidden email]> wrote: I am trying to create the following view: |
|
Thanks Aaron for the help and the info. The change to the controller worked. However, one reason for using the executeQuery was I wanted to be able to dynamically alter the from and where clause rather than returning the entire list. The select statement was just kept simple to eliminate errors in the select statement to start with. I would like to try and figure out how to use the executeQuery if possible.
|
|
Well, you are creating an HQL query so you don't have to indicate the columns that you want. Once you provide the select part of the query, you are going to get an Array instead of a GORM object.
So, doing Roles.executeQuery("from Roles where ....") should return a list of Role objects but Roles.executeQuery("select ... from Roles") will return an Array.
In your first example, you didn't have the "id" field in your query, so it wouldn't be in the array, which may have caused the error. -Aaron
On Fri, Jun 15, 2012 at 5:02 PM, stuart.simpson <[hidden email]> wrote: Thanks Aaron for the help and the info. The change to the controller |
|
Thank makes sense. I appreciate your help. From: Aaron Long [via Grails] [mailto:[hidden email]] Well, you are creating an HQL query so you don't have to indicate the columns that you want. Once you provide the select part of the query, you are going to get an Array instead of a GORM object. So, doing Roles.executeQuery("from Roles where ....") should return a list of Role objects but Roles.executeQuery("select ... from Roles") will return an Array. In your first example, you didn't have the "id" field in your query, so it wouldn't be in the array, which may have caused the error. -Aaron On Fri, Jun 15, 2012 at 5:02 PM, stuart.simpson <[hidden email]> wrote: Thanks Aaron for the help and the info. The change to the controller |
| Powered by Nabble | Edit this page |
