Silverstripe:按作者筛选博客文章

Dal*_*lby 5 php silverstripe

我现在谷歌搜索大约2个小时,无法找到这个问题的答案.我正在尝试按作者/会员ID过滤博客帖子(使用silverstripe-blog模块).到目前为止我有:

public function MyRecentPosts() {

        $posts = BlogPost::get()
            ->sort('PublishDate','desc')
            ->limit(2);

        return $posts;
    }
Run Code Online (Sandbox Code Playgroud)

显然,只返回最新的博客帖子.我不确定我是否理解如何将Blog Post表与BlogPost_Authors表相关联...

任何建议将不胜感激.

Rob*_*ill 7

那么它BlogMemberExtension被应用于Member类,它为您提供了一种通过"很多很多"关联访问成员帖子的简单方法.

我在这里假设这个函数不会在Member的扩展名中,并且你将传递成员ID,因为它已经不存在于你的代码中.这个假设可能不正确,因为你的方法被命名为" My RecentPosts",但无论如何 - 这是一个例子:

public function MyRecentPosts($memberId)
{
    $member = Member::get()->byId($memberId);

    $posts = $member->BlogPosts()
        ->sort('PublishDate', 'desc')
        ->limit(2);

    return $posts;
}
Run Code Online (Sandbox Code Playgroud)

你也可以BlogPost通过它的"很多"协会从模型中做到这一点:

$posts = BlogPost::get()
    ->filter(array('Authors.ID' => $memberId))
    ->sort('PublishDate', 'desc')
    ->limit(2);
Run Code Online (Sandbox Code Playgroud)