How to get value in my show page in Grails - grails

How to get the value as following codes with my show view page on Grails?
Person.groovy
package com
class Person {
String person
static constraints = {
person blank:false,nullable:true
}
static hasMany=[task:Task]
String toString(){return person}
static mapping={
}
}
Task.groovy
package com.moog
class Task {
String task
static constraints = {
task blank:false,nullable:true,unique:true
}
static belongsTo=[person:Person]
static hasMany=[tag:Tag]
String toString(){return task}
}
Tag.groovy
package com
class Tag {
String tag
static constraints = {
tag blank:false, nullable:true
}
static belongsTo=[task:Task]
String toString(){
return tag
}
}

First of all try a better wording for your collections
static hasMany=[tasks:Task] // in Person.groovy
static hasMany=[tags:Tag] // in Task.groovy
In your person show.gsp try something like
<g:each in=${person.tasks} var="task">
<p>${task}</p>
</g:each>
If you do not use scaffolding and write you own controller methods to create your entities than maybe this helps you further:
def task = new Task(task:"Clean room")
def person = Person.get(1)
person.addToTasks(task)
person.save()

Related

Grails search from one to many is not working

Here are my domain class,
class Company {
String name
static hasMany = [groups:CompanyGroup]
}
class CompanyGroup{
String name
static belongsTo = [company:Company]
}
I receive params that contain name of CompanyGroup and I want to get the result of company that have the CompanyGroup found.
I did like this,
def groupList = account.companies.groups.flatten()
def groupResult = groupList.findAll{
it.name ==~ /(?i).*${params.keyword}.*/
}
I got the Companygroups that have name from params.key from above code. So I want to render company list that have these group like this,
def com = Company.withCriteria{
eq("groups", groupList)
}
render [companies : com]
It doesn't work!
def com = Company.withCriteria{
inList("groups", groupList)
}

grails: sort by nested attributes

Is it possible to sort by nested attributes using where queries?
I have 2 domain classes:
class Parent {
String name
Child child
}
and
class Child {
String name
static belongsTo = [parent: Parent]
}
This works:
Parent.where {}.list(sort: 'name')
and this doesn't:
Parent.where {}.list(sort: 'child.name')
I have an error:
could not resolve property: child.name of: Parent
I am using grails 2.3.x
See this: Grails - sort by the domain relation attribute (using createCriteria())
Solution 1:
def criteria = Child.createCriteria();
println criteria.list{
createAlias("parent","_parent")
order( "_parent.name")
}
Solution 2:
def criteria = Child.createCriteria();
println criteria.list{
parent {
order("name")
}
}
Solution 3:
class Child {
String name
static belongsTo = [parent: Parent]
public String getParentName(){
return parent.getName()
}
}
println Child.listOrderByParentName()
Hope it helps.

Inheritance and data binding on Grails

I have a problem to use data binding with inherit models. That's my code:
class Member {
int age
static belongsTo = [Family]
static constraints = {}
}
class Parent extends Member {
String name
static constraints = {}
}
class Family {
String name
List members
static hasMany = [members: Member]
static constraints = {}
}
def test(){
def bindingMap = [name: 'toto', members:[[age: 18, name: '1'],[age: 18]]]
def family = new Family()
family.properties = bindingMap
family.save(flush: true)
def get = Family.get(family.id)
// here family only contains Member and no Parent, as expected...
}
With this code, as you can see, I'm not able to create Parent with data binding.
Anyone have an idea?
Thanks

Grails: Paging and Sorting over the collection in a relationship

I'd like to do paging and sorting from a collection in a relationship
For example with the following model:
class User {
String userName, password
static hasMany = [roles: UserRole, preferences: Preference]
}
class UserRole {
String name, description
static hasMany = [actions: Action]
}
I'd like to recover all the roles for a specific user. I already have the user loaded so the normal way to do it would be using
user.roles
But I want to sort them by UserRole properties and I want to paginate them dynamically
I know that if I want to get all the UserRoles sorted and paginated I can use:
UserRole.list([sort: 'name', order: 'asc',max: 5,offset:0])
But I want to do it just for the roles that are associated to my user. I was trying to use criteria, but I think I'm missing something.
I also had a look here:
http://grails.1312388.n4.nabble.com/A-Relationship-Paging-Pattern-td1326643.html
But then I would have to add the relation back into UserRole so I would have:
static hasMany = [users : UserRole]
How can I do this? what would be the best way?
Please, let me know if you need more information and sorry if I wasn't clear enough
Thanks and regards
You cannot paginate an "ordinary" relationship.
You can change the order child objects appear in using mapping DSL:
static mapping = {
sort name:desc
}
To simplify a hand-crafted paginated relationship, you can use a named query:
class Role {
static namedQueries = {
userRoles {
eq('user', UserSessionService.instance.currentUser)
}
}
}
Or you can implement a transient User's property that will return a Criteria for User's Roles (which can be paginated).
Grails Pagination with hasmany relation Bidirectional property finally i come to the point were i found it working Huuuh.
These are the Domain classes
class Client {
List bills
String shopName
String nameOfClient
static hasMany = [bills: Bill]
static constraints = {
shopName(blank:true, nullable:true)
nameOfClient(blank:false, nullable:false)
}
}
class Bill {
String billDetails
String billNo
static belongsTo = [client: Client]
static constraints = {
billDetails(blank:true, nullable:true , type: 'text')
billNo(blank:true, nullable:true)
}
}
Now This is my controller Logic
def clientDetails(){
def maxJobs = 4
def offset = (params?.offset) ?: 0
def clientId = params.id
def bills = Client.get(clientId).bills
def client= Client.get(clientId)
def results = Bill.withCriteria {
eq('client', client)
firstResult(offset as Integer)
maxResults(maxJobs)
}
[id:client.id,bills: results, offset: offset, max: maxJobs, totalJobs: bills.size()]
}
And the gsp code
<g:each in="${bills}">
<tr>
<td>${it.billNo}</td>
<td>${it.billDetails}</td>
</tr>
</g:each>
<g:paginate class="pagination" controller="client" action="clientDetails" total="${totalJobs?:0}" offset="${offset}" max="${max}" params="[id:"${id}"]"
prev="« Previous" next="Next »" />

One's more about grails searchable plugin

I have two simple domains:
public class Hotel {
static searchable = true
Source source
City city
HotelType type
long sourceid
float lat
float lon
static hasMany = [hotelTexts:HotelText]
static mapping = {
hotelTexts batchSize:10
}
}
public class HotelText {
static searchable = true
static belongsTo = [hotel:Hotel]
String lang
String name
String description
String address
static mapping = {
batchSize:10
description type:"text"
}
}
I'm totally new in searchable plugin but i believe that it could help me with my problem.
So, the task is to find Hotels by city and then sort result by name. Without sorting it could be easily done with dynamic finders help but...
Summary:
Find hotels by city.
Sort result by hotel name(for given language).
Support pagination.
public class Hotel {
static searchable = {
hotelTexts component: true
}
...
}
public class HotelText {
static searchable = {
name boost: 2.0
}
...
}

Resources