在Silverstripe 3.0.5中,我试图在ShortCode函数中查询来自many_many Relation的图像。当我使用SQLQuery()查询时,我能够看到(调试)所有数据,但是不知道如何使用ImageWidth或CroppedImage这样的Image / GD函数,当我不查询每个SQLQuery时,我通常可以使用模板中的图像。我可以直接在模板中引用$ 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
...
答案 0 :(得分: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));
}