如何在OctoberCMS中覆盖模型属性和方法?

时间:2019-10-07 04:42:03

标签: laravel octobercms octobercms-plugins

我想通过更改属性 $allowedSortingOptionsscopeListFrontEnd() 范围来扩展Blog插件。

该属性定义了可用的排序选项,我想添加一些其他选项。 来自 Blog Post.php的原始值:

    public static $allowedSortingOptions = [
        'title asc'         => 'rainlab.blog::lang.sorting.title_asc',
        'title desc'        => 'rainlab.blog::lang.sorting.title_desc',
        'created_at asc '   => 'rainlab.blog::lang.sorting.created_asc',
        'created_at desc'   => 'rainlab.blog::lang.sorting.created_desc',
        'updated_at asc'    => 'rainlab.blog::lang.sorting.updated_asc',
        'updated_at desc'   => 'rainlab.blog::lang.sorting.updated_desc',
        'published_at asc'  => 'rainlab.blog::lang.sorting.published_asc',
        'published_at desc' => 'rainlab.blog::lang.sorting.published_desc',
        'random'            => 'rainlab.blog::lang.sorting.random'
    ];

我还想扩展范围逻辑。 来自 Blog Post.php的原始方法:

    public function scopeListFrontEnd($query, $options)
    {
      // Code ...
      return $query->paginate($perPage, $page);
    }

在我的插件中,我尝试了几种方法在Plugin.php文件中执行此操作。

private function extendModel()
    {
        // Code ...

        PostModel::extend(function ($model) {
            // Code ...

            // This does not work
            // I want to override $setAllowedSortingOptions here and add other options
            $model::$allowedSortingOptions = [
                // My Options
            ];

            // This does not work either
            $model->addDynamicProperty('allowedSortingOptions', $MyOptions);

            // I want to retrieve the result query of the scope ListFrontEnd to change it
            Event::listen('backend.filter.extendQuery', function($filterWidget, $query, $scope) {
                if ($scope->scopeName == 'listFrontEnd') {
                    // Change the $query order here
                }
            });
        });
    }
  • 那么,如何覆盖Class属性?
  • 如何扩展/覆盖范围或方法?

谢谢。

0 个答案:

没有答案