我需要一些帮助,将此代码从SS3转换为SS4。 我使用下面的代码将最新的博客文章拖到我的自定义主页模板中。这在SS4中不再对我有用。不知道如何添加需要修复的内容。
class IndexPageController extends PageController {
public function LatestPostsHome()
{
return BlogCategory::get()
->filter('Title', 'Featured')
->relation('BlogPosts')
->sort('PublishDate', 'DESC');
}
}
谢谢
答案 0 :(得分:1)
您是否有几个标题相同的BlogCategories?
如果您只有一个标题为“精选”的BlogCategory,则此方法应该有效:
public function LatestPostsHome()
{
$blogCategory = BlogCategory::get()->filter('Title', 'Featured')->first();
if (!$blogCategory) {
return null;
}
// Get the corresponding has_many/many_many objects.
$blogPosts = $blogCategory->BlogPosts()->sort('PublishDate', 'DESC');
return $blogPosts;
}
如果您有多个具有相同标题的类别,则可以使用以下内容:
public function LatestPostsHome()
{
$blogCategories = BlogCategory::get()->filter('Title', 'Featured');
if (!$blogCategories->exists()) {
return null;
}
// Option 1 (not tested)
$categoryIDs = $blogCategories->column('ID');
$blogPosts = BlogPost::get()->byIDs($categoryIDs);
return $blogPosts;
// Option 2 (not tested)
$blogPosts = new \SilverStripe\ORM\ArrayList();
foreach ($blogCategories as $category) {
$posts = $category->BlogPosts();
$blogPosts->push($posts->toNestedArray());
}
return $blogPosts;
}