2012-11-29 6 views
0

편집 -Grails의 PARAMS

내 2 개 도메인 클래스에서 각 포트폴리오에 부착 된 지난 5 출판물과 함께, 포트폴리오의 목록을 반환하려고 2.1 createCriteria 문제. 마지막으로 총 5 개의 발행물이 반환되며 각 목록에는 모두 5가 표시됩니다. 쿼리는 해당 특정 인스턴스를 자체 발행물로 반환하지 않습니다. Kellys의 훌륭한 아이디어는 다른 트랙으로 되돌아갑니다.

나는 portfolioTo 클래스에 속하는 출판물의 hasMany 쪽 인 포트폴리오 컨트롤러에서 메소드를 만들었습니다.

난 그냥 자신의 출판물을 나열하는 포트폴리오를 얻을 것 같습니다. eq 포트폴리오를 변경하면 각 포트폴리오 목록에 같은 발행물이 표시된다는 점을 제외하고는 모두 잘 작동합니다.

각 포트폴리오를로드하고 마지막 5 개의 발행물을 나열하려면 어떻게합니까? 이것은 포트폴리오/목록 페이지에서 부분적으로 렌더링됩니다. 이것이 문제 일 수 있습니다. 포트폴리오 목록 작업과 관련이없는 새로운보기에서 렌더링해야합니까 ??

초보자가 grails을 읽고 문서를 읽었으며 제대로 돌아 오지 않는 params 쿼리를 얻는 것 같습니다. 도움

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의 최대 절전 기준을 사용하면 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> 

편집 -

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 

답변

1

portfolios은 (아마도)이 Publication 클래스의 목록과 portfolio입니다하지 않은 java.lang.ClassCastException 때문에 당신은 점점 아래 SQL 로그, 그것은 아마이다 id (long); 의미있는 방식으로 길게 비교할 수있는 목록을 전송할 수 없음 eq ('portfolio', portfolios)

도메인 클래스가 관련되어 있으므로 두 개의 별도 쿼리가 필요하지 않습니다.

- EDIT-- 편집 작업은 별도의 작업을 사용하지 않고 목록 동작 만 사용하십시오. 나는 include을 작동시키지 못했지만, 아래의 경우는 수십 가지에 불과합니다. 만약 당신이 이것을 할 수없는 이유가 있다면 어쩌면 include 메커니즘을 사용하는 것에 관한 새로운 질문이 약간의주의를 끌 수도 있습니다.

현재 목록 동작이 어떤 모양인지 잘 모르겠습니다. 이것은 모든 포트폴리오와 마지막 5 개의 출판물을 얻기위한리스트 방법을 코딩하는 방법입니다. 모든 포트폴리오를 반환하기 때문에 매개 변수가 필요하지 않습니다.

//PortfolioController 
def list(){ 
    def portfolios = Portfolio.createCriteria().list { 
     //if you needed to filter the list by for example portfolio status or something you could add that here 
     or { 
      eq('status','ACTIVE') 
      eq('status','PENDING') 
     } 
     publications(org.hibernate.criterion.CriteriaSpecification.LEFT_JOIN) { 
      eq("published", "Yes") 
      order("lastUpdated", "desc") 
      firstResult(5) 
     } 
    } 

    [portfolios: portfolios, portfolioCount:portfolios.size()] 
} 

이제 해당 간행물은 포트폴리오에 미리 첨부되어 있습니다.

위의 LEFT_JOIN 부분은 조건을 충족하는 첨부 된 게시물 만있는 포트폴리오 목록을 되 찾을 것을 보장합니다. 만약 당신이 그것을 내버려두면 기본적으로 내부 조인이되며, 반복 할 때 포트폴리오에 대한 모든 출판물을 얻을 수 있습니다 (기준을 충족시키지 못하더라도).

그런 다음 gsp에서 포트폴리오를 반복합니다. list.gsp에 직접 입력하거나 list.gsp으로 렌더링 한 템플릿으로 보낼 수 있습니다. 당신이라는 템플릿에 넣어 경우 _webList.gsp 당신이 중 하나를 list.gsp 또는 _webList.gsp에있는 list.gsp

<g:render template="weblist" model="['portfolios': portfolios]" /> 

로에서 렌더링 것이다 - 나는 그것이 모든 작동하는지 확인하기 위해 list.gsp에서 직접 시작하는 것입니다.
<g:each in="${portfolios}" var="portfolioInstance" status="i"> 
    ${portfolioInstance?.portfolioName?.encodeAsHTML() 
    <!-- whatever else you need here --> 
    <!-- now iterate through the pubs --> 
    <g:each in="${portfolioInstance.publications"} var="publicationInstance" status="j"> 
     ${publicationInstance.id} 
     <!-- and again whatever else you need here --> 
    </g:each> 
</g:each> 

--EDIT-- firstResult(5)

트릭을 할 것으로 보인다. --EDIT--

내가 maxResults(5)에 댓글을 달았습니다. 제대로 작동하는 데 문제가 있습니다. 연결 블록에 있어도 반환되는 포트폴리오 수를 제어하는 ​​것으로 보입니다. 어쩌면 다른 사람이 이것을보고 퍼즐 조각을 추가하거나 자신과 함께 팅커를 추가 할 것입니다. 내가 알아 내면 계속 노력하겠습니다.

+0

감사합니다.이 말은 나에게 의미가 있습니다. 루비 세계에서 왔을 때, 나는 그 기준에 문제가있었습니다. 당신은이 짧은 공간에서 많은 것을 설명했습니다. 정말 감사. 나중에 다시 시도해보고 결과를 알려 드리겠습니다. – IanN

+0

좋아, 내가 알아 냈다고 생각해. 'firstResult (5)'를 사용하는 답변을 업데이트하고 원하는 모든 것을 수행하는 것으로 보이는 내 테스트에서! 이게 작동하면 알려주세요 ... – Kelly

+0

Kelly, Ok SQL 로그가 쿼리 작업을 표시하지만 gsp 반복이 아무것도로드하지 않습니다. 뷰 코드는 포트폴리오 디렉토리 "_weblist.gsp"내의 부분 템플릿에 있음을 언급해야합니다. 나는 포트폴리오/list.gsp 다음 코드를 표시하지 않는다. – IanN