編輯 -Grails的2.1個createCriteria問題使用參數
我試圖返回的投資組合列表,從我的2域類連接到每個投資組合在過去5個出版物一起。我得到最後總共5個出版物,每個列表顯示全部5.查詢沒有返回特定實例自己的出版物。凱利偉大的想法回到了另一個軌道。
我已經在投資組合控制器中創建了該方法,它是屬於投資組合域類的出版物的hasMany方面。
我似乎無法讓投資組合列出他們自己的出版物。如果我更改了eq投資組合,它一切正常,除了每個投資組合列表顯示相同的出版物。
如何加載每個投資組合並列出最後5個出版物。這是從組合/列表頁面作爲一個部分渲染出來的。也許是這個問題。我應該從一個與組合列表動作無關的新視圖中渲染它嗎?
新手到grails並閱讀和閱讀文檔的,只是不能得到參數查詢返回正確。幫助
def _webList(){
//def per = Portfolio.properties
def portfolios = Portfolio.list(params.id)
def results = Publication.withCriteria {
eq('published', 'Yes')
order('lastUpdated', 'desc')
maxResults(5)
}
def reportscount = Publication.count()
[ portfolios: portfolios, results: results, reportscount: reportscount]
}
如果需要,我可以顯示sql日誌。
EDIT
下面的代碼是從文件_webList.gsp整個部分。頁面頂部div -alert加載,但div屬性列表中的內容無法加載。使用Kelly的hibernate標準會在sql日誌中產生查詢,但不會返回結果或樣式,或者返回視圖。奇怪的。!
<div class="alert alert-info" xmlns="http://www.w3.org/1999/html">Permissions apply to <strong>editing</strong> publications.<br>
<div style="display: inline;"><p>Click portfolio name to read or edit publications. Total number of sites: <strong>${rsNumb}</strong> | Total number of publications: <strong>${reportscount}</strong> </p>
</div>
</div>
<div class="property-list portfolio">
<g:each in="${portfolios}" var="portfolioInstance">
<div class="site-listing">
<div><span class="label">Site Name:</span><g:link action="show" id="${portfolioInstance?.id }">${portfolioInstance?.portfolioName?.encodeAsHTML()}</g:link></div>
<div><span class="label">Site Description: </span>${portfolioInstance?.portdescrip?.encodeAsHTML() }</div> <br>
<div><span class="label">Site Administrator: </span>${portfolioInstance?.profile?.portfolioAdmin?.encodeAsHTML() }</div> <br>
<div><span class="label"> Total publications:</span><span class="badge badge-success"> ${portfolioInstance?.publications?.size()}</span> </div>
<!-- whatever else you need here -->
<!-- now iterate through the pubs -->
<g:if test="${portfolioInstance?.publications}">
<g:set var="publicationInstance" />
<ul class="site-publication">
<li class="fieldcontain">
<span id="publications-label" class="property-label"><g:message code="portfolio.publications.label" default="Last 5 published publications:" /></span>
<g:each in="${portfolioInstance.publications}" var="publicationInstance">
${publicationInstance?.id}
<span class="property-value" aria-labelledby="publications-label"><g:link controller="publication" action="show" id="${publicationInstance.id}">${publicationInstance?.encodeAsHTML()}</g:link></span>
<!-- and again whatever else you need here -->
</g:each>
</g:if>
</g:each>
</div>
編輯 - SQL日誌下面
Hibernate: select this_.id as id5_1_, this_.version as version5_1_, this_.date_created as date3_5_1_, this_.last_updated as last4_5_1_,
this_.portdescrip as portdesc5_5_1_, this_.portfolio_name as portfolio6_5_1_, this_.portpublished as portpubl7_5_1_, this_.profile_id as profile8_5_1_,
this_.status as status5_1_,
publicatio1_.portfolio_id as portfolio5_5_3_,
publicatio1_.id as id3_, publicatio1_.id as id2_0_,
publicatio1_.version as version2_0_,
publicatio1_.date_created as date3_2_0_,
publicatio1_.last_updated as last4_2_0_,
publicatio1_.portfolio_id as portfolio5_2_0_,
publicatio1_.publication_content as publicat6_2_0_,
publicatio1_.publication_name as publicat7_2_0_,
publicatio1_.published as published2_0_,
publicatio1_.publisheddate as publishe9_2_0_,
publicatio1_.publishedemail as publish10_2_0_,
publicatio1_.pubproduct_id as pubproduct11_2_0_
from portfolio this_ left outer join publication publicatio1_
on this_.id=publicatio1_.portfolio_id where (this_.status=?)
and (publicatio1_.published=?) order by publicatio1_.last_updated desc
謝謝隊友,這對我很有意義。來自紅寶石世界,我遇到了這個標準的問題。你在這個短的空間裏已經解釋了很多。對此,我真的非常感激。我會稍後嘗試,並讓你知道結果。 – IanN
好吧,我想我明白了。更新了使用'firstResult(5)'的答案,並在我的測試中似乎做了你想做的一切!讓我知道這是否工作... – Kelly
凱利,好,所以SQL日誌顯示查詢工作,但gsp迭代不加載任何東西。我應該提到視圖代碼位於投資組合目錄「_weblist.gsp」中的部分模板中。我在portfolio/list.gsp中有以下代碼奇怪,它不顯示 –
IanN