0

在 Silverstripe 3.0.5 中,我试图从 ShortCode 函数中的 many_many 关系中查询图像。当我使用 SQLQuery() 进行查询时,我能够看到(调试)所有数据,但不知道如何使用像 SetWidth 或 CroppedImage 这样的 Image/GD 函数,当不按 SQLQuery 查询时,我通常可以在模板中使用这些函数. 我可以直接在 Template 中引用 $Filename。但是有没有办法以我查询数据的方式在模板中使用图像处理函数(SetWidth、CroppedImgae ...),或者我应该如何在 GalleryShortCodeHandler 中获取 GalleryImages 才能这样做?

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
...    
4

1 回答 1

1

您最好留在 ORM 上的领域中,以便模板解析器将 GalleryImage 对象理解为 Images 对象,然后您就可以使用标准的图像处理功能。也许是这样的:

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));
}
于 2013-04-28T20:15:22.380 回答