- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想构建非常简单的帮助台应用程序来学习 grails。我希望用户能够登录,创建请求/事件。
用户选择产品线、该产品线的主题和该主题的子主题。每个子主题都有一个或多个对其负责的支持用户。
另一方面,支持用户可以查看属于他“管辖范围”的事件列表。我的域类看起来像这样:
class Request{
Date dateCreated
String subject
String startedBy
String description
String status
String priority
Productline productline
Topic topic
Subtopic subtopic
static constraints = {
//id unique: true
dateCreated(blank:true,nullable:true)
subject()
description(maxSize:5000)
status (blank:true,nullable:true)
priority(inList:["Normalan","Hitno","Nije hitno"])
productline(blank:true,nullable:true)
topic(blank:true,nullable:true)
subtopic(blank:true,nullable:true)
company(blank:true,nullable:true)
contact(blank:true,nullable:true)
startedBy(blank:true,nullable:true)
acceptedBy(blank:true,nullable:true)
}
String subtopic
Productline productline
Topic topic
static hasMany = [responsibilities: Responsibility]
String toString(){
"$subtopic"
}
static constraints = {
subtopic()
productline()
topic()
}
List contacts(){
return responsibilities.collect{it.contact}
}
List addToContacts(Contact contact){
Responsibility.link(contacts, this)
return contacts()
}
List removeFromContacts(Contact contact){
Responsibility.unlink(contact, this)
return contacts()
}
class Responsibility {
Contact contact
Subtopic subtopic
static Responsibility link(contact, subtopic){
def r = Responsibility.findByContactAndSubtopic(contact, subtopic)
if(!r){
r = new Responsibility()
contact?.addToResponsibilities(r)
subtopic?.addToResponsibilities(r)
}
return r
}
static void unlink(contact, subtopic){
def r = Responsibility.findByContactAndSubtopic(contact, subtopic)
if(r){
contact?.removeFromResponsibilities(r)
subtopic?.removeFromResponsibilities(r)
r.delete()
}
}
static constraints = {
}
class User {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
String toString(){
"$username"
}
static constraints = {
username blank: false, unique: true
password blank: false
}
static mapping = {
password column: '`password`'
}
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
class Contact {
String realname
String company
String mobile
String fix
String email
User user
String toString(){
"$realname"
}
static hasMany = [responsibilities: Responsibility]
static constraints = {
}
List subtopics(){
return responsibilities.collect{it.subtopic}
}
List addToSubtopics(Subtopic subtopic){
Responsibility.link(this, subtopic)
return subtopics()
}
List removeFromSubtopics(Subtopic subtopic){
Responsibility.unlink(this, subtopic)
return subtopics()
}
def supportList = {
def contact = Contact.findByUser(springSecurityService.currentUser)
def requestList = contact.subtopics().collect {Request.findAllBySubtopic(it)}
params.max = Math.min(params.max ? params.int('max') : 10, 100)
[requestInstanceList: requestList, requestInstanceTotal: requestList.count()]
}
<%@ page import="com.testapp.Request" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="layout" content="main" />
<g:set var="entityName" value="${message(code: 'request.label', default: 'Request')}" />
<title><g:message code="default.list.label" args="[entityName]" /></title>
</head>
<body>
<div class="nav">
<span class="menuButton"><g:link class="create" action="create">
<g:message code="default.new.label" args="[entityName]" /></g:link></span>
</div>
<div class="body">
<h1>Lista Zahteva</h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<div class="list">
<table>
<thead>
<tr>
<g:sortableColumn property="id"
title="${message(code: 'request.id.label', default: 'Id')}" />
<g:sortableColumn property="subject" title="Datum kreiranja" />
<g:sortableColumn property="subject" title="Tema" />
<g:sortableColumn property="priority" title="Prioritet" />
<th><g:message code="incident.startedBy" default="Započeo" /></th>
<g:sortableColumn property="status" title="Status" />
</tr>
</thead>
<tbody>
<g:each in="${requestInstanceList}" status="i" var="requestInstance">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td><g:link action="show" id="${requestInstance.id}">
${fieldValue(bean: requestInstance, field: "id")}</g:link></td>
<td width="200">${fieldValue(bean:requestInstance,field:"dateCreated")}</td>
<td width="200">${fieldValue(bean: requestInstance, field: "subject")}</td>
<td width="200">${fieldValue(bean: requestInstance, field: "priority")}</td>
<td width="200">${fieldValue(bean: requestInstance, field: "startedBy")}</td>
<td width="200">${fieldValue(bean: requestInstance, field: "status")}</td>
</tr>
</g:each>
</tbody>
</table>
</div>
<div class="paginateButtons">
<g:paginate total="${requestInstanceTotal}" />
</div>
</div>
</body>
</html>
最佳答案
使用您当前的模型,您可以像这样查询当前用户职责:
Responsability.findAllBySupport(springSecurityService.currentUser).
collect {Incident.findAllBySubtopic(it.subtopic)}.flatten()
class User {
static hasMany = [supportedSubtopics: Subtopic]
...
}
class SubTopic {
static hasMany = [supportingUsers: User]
....
}
springSecurityService.currentUser.supportedSubtopics.collect {Incidents.findBySubtopic(it)}.flatten()
hasMany = [incidents: Incident]
到子主题,您可以进一步将其简化为
springSecurityService.currentUser.supportedSubtopics*.incidents.flatten()
关于grails - 查询 GORM 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8181993/
我在使用 Go 进行应用程序开发,我使用 gorm 库连接到数据库。我看到 gorm 有 2 个库 github.com/jinzhu/gorm和 gorm.io/gorm . 我不知道该用哪个。它们
我是 Gorm 的新手。我正在尝试进行级联删除,如果我删除一个用户,则与该用户关联的角色(属于)、个人资料(有一个)和书籍(一对多)也将被删除。 我在下面设置了我的模型,但级联似乎不起作用。当我删除我
使用 Jinzhu 的 GORM 包,顺便说一句,我现在有这个结构: type User struct { gorm.Model // The Users username U
Golang 的 GORM 库支持复合主键。但是如何从相关模型中引用它们呢? 例如,假设我有一个 User 和一个 Note 模型: type User struct { Organizati
我正在尝试使用 Grails 项目作为插件,基本上将我的域类放在插件中,然后在多个 Grails 项目中使用它们。 我已经这样做了: grails 创建应用程序网页 grails 创建应用程序插件 在
Golang 的 GORM 库支持复合主键。但是如何从相关模型中引用它们呢? 例如,假设我有一个 User 和一个 Note 模型: type User struct { Organizati
如何省略来自 gorm.Model 的列。例如。创建日期、修改日期。 考虑以下模型: type User struct{ gorm.Model Firstname string
当我在 Grails 中执行 domainObj1 == domainObj2 时,对象是按 ID 进行比较的吗?如果不是,如何比较? 最佳答案 首先,您需要了解 GORM/Grails 在 equa
目前正在为此苦苦挣扎。 我希望能够使用抽象域类来使我能够使用一些通用代码来执行一些常用操作。 我的问题是很多 GORM 操作都是域类上的静态方法,这使得它变得困难。想知道这些方法是否有任何非静态等价物
我有一个结构/模型 type User struct { gorm.Model Name string `gorm:"unique;not null"
我想创建一组在我的应用程序中使用的 gorm 类型。所以我想用我的类型定义一个 map gorm.DB作为键和空的结构{}作为标志: var ( autoMigrations map[gorm
有表 customer_account (postgres) 是从 YII2 迁移过来的。 数据链接: CREATE TABLE public.test_table ( id INTEGER PR
我有一个域对象,它已经有一个名为 versions 的属性,所以我想给内置的 version 属性一个不同的名称(在 GORM 中用于乐观锁定)。例如,我想将其称为 updateCount。 请注意,
我有一个普通的 Grails 3.3.2 应用程序。我可以使用 gradle testintegrationTest 很好地运行测试。但是,当我尝试右键单击并在 IntelliJ 中运行测试类或单个测
除了这样做对性能的明显影响之外,还有什么好的技术原因 不是 设置 grails.gorm.autoFlush = true和 grails.gorm.failOnError = true在 Confi
GORM 似乎不尊重我基于外键关联表的尝试,而我的主键关联有效。 我有 3 个结构,它们与 3 个单独的数据库表相关联。它们的关系如下: Inventory.CustID 与 Customer.Cus
我有一个像这样的 golang 结构的表: Order { ID TransactionID Transaction } Transaction { ID ProfileID
我正在使用 Gorm,对如何从模型中检索嵌套的 SubComments 有一些疑问。我遇到的问题是评论嵌套了两层深,即 Comment.SubComments 没有加载。我是否遗漏了 Preload
我正在尝试使用 golang gorm 创建一个自我引用的一对多关系。每个用户可以创建多个其他用户 (Created),但一个用户始终由另一个用户创建 (UserID)。 type User stru
假设我有 2 个表,它们共享一些列名,例如: table_1 - id - created_at - deleted_at - name - color table_2 - id - created_
我是一名优秀的程序员,十分优秀!