How to Alphabetically retrieve members of a list? - grails-orm

I have an organization class
class Organization {
hasMany = [member:Members]
}
class Members {
belongsTo = organization
}
I'm printing all the members using
<ol>
<g:each in="${organizationInstance?.members?}" var="m">
<li><g:link controller="members" action="show" id="${m.id}">${m?.encodeAsHTML()}</g:link></li>
</g:each>
</ol>
I want to sort the printing of members so that it would print alphabetically.
any ideas?

First, you need to change somehow your classes in order to have a name for members !
So let's assume that your classes are:
class Organization {
hasMany = [members:Member]
}
class Member {
belongsTo = organization
String name
}
Then you have two ways of sorting the members in alphabetical order.
First method : you can retrieve all members and then sort them as shown below:
<g:each in="${organizationInstance?.members?.sort {it.name} }" var="m">
Second Method : You retrieve members directly from GORM in alphabetical order
def members = Member.findAllByOrganization(organizationInstance, [sort: "name"])

Related

Use find() and with() together in Laravel query

I have 2 tables employees and employee_locations. One employee has many locations. I need to find out one employee record with related latest employee_locations record. I wrote below query.
$employees = Employee::find([1])->with('employees.employee_locations')->latest()->first();
I am getting below error
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::with does not exist.
Your issue is that the find method retrieves a collection of Eloquent objects, on which the with method can not be used. You must first specify your relations for the Employee objects and then use find.
The below code will retrieve the employee with the ids specified in find method and locations for each employee:
$employees = Employee::with('employees.employee_locations')->find([1])
create a relation in your model. Something like this:
class Employee extends Model
{
protected $table = 'employees';
public function location()
{
return $this->hasMany(EmployeeLocation::class, 'employeed_id');
}
}
class EmployeeLocation extends Model
{
protected $table = 'employee_locations';
}
$employees = Employee::with('location')->first();
or you do
$employees = Employee::with('location')->find(<employeed_id>);
Try This method
$employees = Employee::with('employees')->where('employeesid',$employeesid)-
>get()->find($employeesid);

Can an attribute be used for several categories in ALFA?

A doctor can belong to subjectCat (the user that is trying to gain access) or to resourceCat (the referring physician of a medical examination the subject is trying to access).
As it appears to me, to support both cases I need to define the doctor for each category individually:
namespace subject {
namespace doctor {
attribute id {
category = subjectCat
id = "id"
type = string
}
attribute lastname {
category = subjectCat
id = "lastname"
type = string
}
//and 20 more attributes...
}
}
namespace resource {
namespace doctor {
attribute id {
category = resourceCat //this line is the only difference
id = "id"
type = string
}
attribute lastname {
category = resourceCat //this line is the only difference
id = "lastname"
type = string
}
//and 20 more attributes...
}
}
That's pretty cumbersome and bears a lot of redundancy. Is there anything I can do to avoid that?
You are right. You would redefine the attributes. In a way you are using the same object from your information model (e.g. doctor) but in one case that object (Doctor) acts as a subject. In another, it acts as an object you are protecting e.g.
A doctor can view a medical record of a patient they are assigned to.
An HR staff can view the salary of a doctor.
Yes, it means you would have to define the attribute itself in multiple categories. You can still leverage the namespace structure insofar as the combination of namespace and name remains unique.
You could do acme.user.staff.doctor and attribute name. You could then do acme.object.doctor and attribute name.
Note that Eclipse will let you do autocomplete too:

Sql error when using GORM's dynamic FindAllBy method - #2 is not set

I have those domain classes:
class Person{
String username
}
class Course {
String code
static hasMany = [events:Event]
}
class Event {
Long id
String name
(...)
static belongsTo = [course:Course, parallel:Parallel]
static hasMany = [teachers: Person, students: Person,bought: Exchange, sold: Exchange]
}
And what I want to do is find all the events associated with a student and a course. I did this query:
(...)
println "User: ${person.username}"
println "Course: ${course.code}"
Set<Person> persons = []
persons.add(person)
def events = Event.findAllByCourseAndStudents(course, persons)
}
But that is giving me an error ERROR util.JDBCExceptionReporter - Parameter "#2" is not set; SQL statement:
What am I missing here?
The issue with the second parameter in your query Students is the fact it's an 1:M (one to many) association and querying these associations using dynamic finders is not supported.
Take a look at this quick overview of the various types of querying supported by GORM/Grails to get a better understanding of what kind of options are available to you and when to use them.
As you can see using a criteria is really what you need in this case:
def events = Event.createCriteria().list() {
eq('course', course)
students {
eqId(person.id)
}
}
}
The above only matches against a single person, but you can modify this further to use a list of persons as you intend. I will leave that exercise to you so you can become familiar with how to create and use criteria.

How to Bind Model with variable at Runtime in mvc 4

I have a Model having more than 50 fields and I want to display list of those fields but not all of them.
So I have created two viewbag having list of display field and all values.
Now I have,
<table>
<tr>
#foreach (var v in #ViewBag.columnList)
{
<th>#v</th>
}
</tr>
#foreach (var u in #ViewBag.userlist)
{
<tr>
#foreach (var c in #ViewBag.columnList)
{
<td>#u.c</td>
}
</tr>
}
I want to assign #u.#c to select column to display in table.
public abstract class UserBaseModel
{
public string Name {get;set;}
public string City {get;set;}
}
public class UserModel : UserBaseModel
{
public string State {get;set;}
}
public class AdminModel : UserBaseModel
{
public string Gender {get;set;}
}
These classes show the different user types having different Properties. You could then have a view stongly typed with the base class
#model UserBaseModel
This way you can pass different models to the view depending on the user. You then have a choice how to access those Properties. It might allow you to select out the Property even though there won't be intelisense, I haven't tried.
You could also have partial views for user type, then depending on type of user in questions, load the relevant partial. Point being, you can pass either User to Admin to the view in the example and both will work.

newbie Nhibernate join query in a DetachedCriteria

I have a domain like this:
class Project
{
...
Unit ProjectUnit
}
class Unit
{
...
IList<User> Users
}
class User
{
...
}
I have to get all projects based on one user, so: each Project where Unit.Users contain query user.
How can I translate this to a DetachedCriteria?
This assumes you have an Id property on your User class and you're passing in a User user.
DetachedCriteria query = DetachedCriteria.For(typeof(Project),"Project")
.CreateCriteria("ProjectUnit","Unit")
.CreateCriteria("Users","users")
.Add(Expression.Eq("Id", user.Id));