2013-04-28 1 views
0

Silverstripe 3.0.5에서 ShortCode 함수에서 many_many 관계의 이미지를 쿼리하려고합니다. SQLQuery()를 사용하여 쿼리 할 때 (Debug) 모든 데이터를 볼 수 있지만 SetWidth 또는 CroppedImage와 같은 Image/GD 함수를 어떻게 사용하는지 알지 못합니다. 일반적으로 SQLQuery에 대해 쿼리하지 않을 때 템플릿의 이미지와 함께 사용할 수 있습니다. . 템플릿에서 $ Filename을 직접 참조 할 수 있습니다. 그러나 템플릿에서 이미지 조작 함수 (SetWidth, CroppedImgae ...)를 사용하여 데이터를 쿼리하는 방법 또는 GalleryShortCodeHandler에서 GalleryImages를 어떻게 얻을 수 있습니까?Silverstripe : ShortCodeHandler에서 many_many를 쿼리하고 이미지 조작 함수를 사용하십시오.

class Page extends SiteTree { 
    $many_many = array(
     "GalleryImages"=>"GalleryImage" 
    ); 
... 
    public function GalleryShortCodeHandler($arguments,$caption = null,$parser = null) { 
     $sqlQuery = new SQLQuery(); 
     $sqlQuery->from("GalleryImage"); 
     $sqlQuery->addLeftJoin('Page_GalleryImages','"GalleryImage"."ID" = "Page_GalleryImages"."GalleryImageID"'); 
     $sqlQuery->addLeftJoin('File','"File"."ID" = "Page_GalleryImages"."GalleryImageID"'); 
     $sqlQuery->addWhere('"PageID" = ' . Controller::curr()->ID); 
     $rawSQL = $sqlQuery->sql(); 
     $result = $sqlQuery->execute(); 

     $returnedRecords = new ArrayList(); 
     foreach($result as $row) { 
      $returnedRecords->push(new ArrayData($row)); 
     } 
     $customise = array(); 
     $customise["Images"] = $returnedRecords; 
     // Debug::show($customise); 
     $template = new SSViewer("Gallery"); 
     return $template->process(new ArrayData($customise)); 
    } 
... 

class GalleryImage extends Image { 
    static $db = array(
     "Descrition" => "Text" 
    ); 
    static $belongs_many_many = array( 
     "Pages" => "Page" 
    ); 
... 

Debug output looks like: 
Debug (Page::GalleryShortCodeHandler() in Page.php:211) 
Images = 
    ArrayList 
     ID = 65 
     Descrition = 
     PageID = 17 
     GalleryImageID = 65 
     SortOrder = 1 
     ClassName = GalleryImage 
     Created = 2013-04-24 14:20:28 
     LastEdited = 2013-04-24 14:20:28 
     Name = xyz.png 
     Title = xyz 
     Filename = assets/Gallery/xyz.png 
     Content = 
     ShowInSearch = 1 
     ParentID = 1 
     OwnerID = 2 
...  

답변

1

당신은 이미지 템플릿 파서 개체로 GalleryImage 개체가 이해할 수 있도록 ORM의 영역에 머물고의 더 좋을 수도, 당신은 다음 표준 이미지 조작 함수를 사용할 수있을 것입니다. 이 라인의 내용은 다음과 같습니다.

public function GalleryShortCodeHandler($arguments,$caption = null,$parser = null) { 
    $customise = array(); 
    $customise["Images"] = 
     GalleryImage::get()-> 
     leftJoin('Page_GalleryImages','"GalleryImage"."ID" = "Page_GalleryImages"."GalleryImageID"')-> 
     leftJoin('File','"File"."ID" = "Page_GalleryImages"."GalleryImageID"')-> 
     where('"PageID" = ' . Controller::curr()->ID); 
    // Debug::show($customise); 
    $template = new SSViewer("Gallery"); 
    return $template->process(new ArrayData($customise)); 
} 
+0

@jfbarrois! 이것은 내가 한 것입니다. ORM 측에서 Image가 파일 - 세부 정보를 자동으로 가져 오기 때문에 File to Join은 더 이상 필요하지 않습니다. – munomono