I have this domain class that has a one-to-many relationship as with dynamic scaffolding show below:
Domain:
package mienapp
class Announcements {
String user
String title
String comments
Date dateCreated
static hasMany = [tag: Tags]
static mapping = {
comments sqlType: 'text'
}
static constraints = {
}
}
Controller:
package mienapp
class AnnouncementsController {
def scaffold = true
def index() {
redirect(action: list)
}
}
When controller redirects to list, the table shows all fields defined in announcements class. How can I show the value of field from tags in the table as well?
Assuming your list method returns a model with an Announcements instance as
def list() {
..
[announcementsInstance: announcementsInstance, ...]
}
in your view, you can access tags like so
<g:each in="${announcementsInstance.tag}" var="tag">
${tag.someproperty}
</g:each>
Related
I have been trying to use dynamic scaffolding in groovy on grails to see the list of the Tasks. My domain class is as follows
package projecttracker
import java.util.Date;
class Task
{
String name
String description
Date dueDate
String toString() {
"${name}"
}
static belongsTo = [enduser: EndUser ,project: Project]
static constraints = {
name()
description()
dueDate()
}
}
My controller class is as follows:
package projecttracker
class TaskController {
def scaffold =true
def index() {
redirect(action:list)
}
}
But, whenever I call the index method of the Task controller, the following error occurs:
URI : /ProjectTracker/task/index
Class: groovy.lang.MissingPropertyException
Message: No such property: list for class: projecttracker.TaskController Possible solutions: edit, flash, class
Around line 7 of grails-app\controllers\projecttracker\TaskController.groovy
4: def scaffold =true
5:
6: def index() {
7: redirect(action:list)
8: }
9:}
Can someone tell me, how to define the list method here? Thanks in advance.
Make sure you use a string for the method name, and the scaffold variable must be static, i.e.
static scaffold = true
def index() {
redirect(action:'list')
}
I am having a problem setting up a List as a hasMany relationship to a belongsTo withing an extended class.
With a new Grails 2.4.4 app I have these 3 Domains:
An Author class having many books.
class Author {
List books
static hasMany = [books: Book]
}
A Book class which extends Content
class Book extends Content {
String foo
}
And Content, which has a number of common properties.
class Content {
String title
static belongsTo = [author: Author]
static constraints = {
sort: 'title'
}
}
When running this application I get the following exception:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: test.relationships.Book column: author_id (should be mapped with insert="false" update="false")
I only get this exception when I make books a List.
I think the problem is probably something to do with the fact you're saying that Author has many Books, but the superclass of Book, Content, belongs to an Authur.
What happens if you change your Content and Book classes to this:
class Content {
String title
static constraints = {
sort: 'title'
}
}
class Book extends Content {
static belongsTo = [author: Author]
String foo
}
or, alternatively, change your Author class to this:
class Author {
List books
static hasMany = [contents: Content]
}
I am trying to have a List of String that works in MySql in a Grails domain class.
I have tried the following:
class Catalogue {
List books
String book
static hasMany = [books: book]
}
and
class Catalogue {
List books
}
and
class Catalogue {
String[] books
}
and
class Catalogue {
ArrayList<String> books = new ArrayList<String>()
}
The last three compiles but the entry is not present in MySQL.
There is no table, or column to represent this data in MySQL and I have tried populating the array with data. Still nothing.
Any ideas?
You can achieve this by using hasMany. Furthermore you need to define books as a List since without it you would get a simple Set that does not allow any duplicates.
class Catalogue {
static hasMany = [books: String]
List books
}
Create a domain Book class:
class Book {
String title
String isbn
...
String toString(){
return "${title}"
}
}
and modify your Catalogue as
class Catalogue{
String name
....
static hasMany = [books: Book]
//etc etc
}
I've tried to scaffold dynamically some domains in GRAILS , but after adding items into database they doesn't show up in the listing view (User/index) in this case :
User.groovy
package scaffold_test
class User {
String username
String address
static constraints = {
}
}
UserController.groovy
package scaffold_test
class UserController {
def scaffold=true
def index() { }
}
so after running the application, I can see the User controller in the Grails home page,I can add Users, and I can see the added instances via DbConsole. But i can't list the instances in the User/index view. Thanks !
Delete def index() { } to let the scaffolding generate all of the methods.
I have following domain classes:
class UserGroup {
String name
String description
static hasMany = [users:User]
}
class User {
String name
static hasMany = [userGroups:UserGroup]
static belongsTo = UserGroup
}
Here User and UserGroup have many to many relationaship.but when I save user to usergroup,the user is not updated with that usergroup.Is there a mapping I can use to do that?I am using following code to add users to group:
static def addUserToGroups(def user){
def userGroups = UserGroup.findAll()
userGroups.each {
if(/** condition **/){
it.addToUsers(user)
it.save(flush:true,failOnError:true)
}
}
}