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