我有两个与一对多关系相关的域-一个用户可能有许多关联。我想查看属于该用户的所有关联。我正在使用脚手架插件。所以应该返回AssociationController中的Associations列表的代码看起来像这样:
def index(Integer max) {
respond Association.list(params), model:[associationInstanceCount: Association.count()]
}
在“用户 View ”页面上,我具有以下代码:
<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>
当我按此操作时,随请求一起发送了
user.id
,它在params映射中(我通过debug对其进行了检查),但是由于某种原因,尽管事实上我正在使用该user.id,但Association.list(params)
返回所有关联(针对所有用户)参数。 A也尝试将user.id
重命名为user
,但也不起作用。似乎这样的.list()仅应用于排序,否则我做错了。您能帮我吗?谢谢! 最佳答案
为了澄清给出的答案:list( params )
返回域对象的所有结果,并且仅接受排序(order by)和视口(限制/偏移)参数。它不提供任何where
子句(嗯,除了discriminator
-column之外)
更新:
您可以使用findAllBy*
:
Association.findAllByUserId( params.long( 'user.id' ), params )
或
criteria
(如果需要使查询更复杂):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 - Grails域类带有参数的.list()方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25425887/