带参数的grails域类.list()方法 [英] grails domain class .list() method with params

查看:119
本文介绍了带参数的grails域类.list()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个与一对多关系相关的域 - 一个用户可能有很多关联。我想查看属于这个用户的所有关联。我正在使用脚手架插件。因此,应该返回AssociationController中关联列表的代码看起来像这样:

I have two domains that are connected with one-to-many relation - One User may have many Associations. I want to view all associations that belong to this user. I'm using scaffolding plugin. So code that should return list of Associations in AssociationController looks lile this:

def index(Integer max) {
    respond Association.list(params), model:[associationInstanceCount: Association.count()]
}



<

And on the User view page I have the following code:

<g:form controller="Association" >
    <fieldset class="buttons">
        <g:hiddenField name="user.id" id="user.id" value="${userInstance.id}"/>
        <g:actionSubmit class="list" action="index" value="${message(code: 'default.list.label', default: 'Show Associations')}"/>
    </fieldset>
</g:form>

当我按下这个actionSubmit user.id 与请求一起发送,它在PARAMS地图中(我使用调试进行了检查),但出于某种原因 Association.list(params)返回所有关联(对于所有用户)尽管我在params中使用这个user.id的事实。 A也尝试将 user.id 重命名为 user ,但它也不起作用。看起来像这样.list()应该只用于排序或我做错了什么。你能帮我解决这个问题吗?谢谢!

When I press this actionSubmit user.id is sent with the request and it is within params map (I checked it with debug) but for some reason Association.list(params) returns all Associations (for all users) despite the fact that I'm using this user.id within params. A also tried rename user.id to just user and it didn't work either. Seems like this .list() should be used only for sorting or I'm doing something wrong. Can you help me with this please? Thank you!

推荐答案

澄清给定的答案: list(params)返回域对象的所有结果,并仅接受排序(按顺序排列)和视图端口(限制/偏移量)参数。它不提供任何 where 子句(除了 discriminator -column)

To clarify the given answer: list( params ) returns ALL results for the domain object, and accepts only sorting (order by) and view-port (limit/offset) parameters. It does not provide any where clause (well, apart from discriminator-column)

更新:

您可以使用 findAllBy *

you can use findAllBy*:

Association.findAllByUserId( params.long( 'user.id' ), params )

条件如果您需要使查询更复杂:

or criteria if you need to make your query more complex:

Association.withCriteria{
   eq 'id', params.long( 'user.id' )
   maxResults params.int( 'max' ) ?: 10
   firstResult params.int( 'offset' ) ?: 0
   if( params.sort && params.order ) order params.sort, params.order
}

这篇关于带参数的grails域类.list()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆