【问题标题】:October CMS - Custom Plugin: JSON Recursion Detected十月 CMS - 自定义插件:检测到 JSON 递归
【发布时间】:2021-08-01 14:09:59
【问题描述】:

我想开发一个在October CMS 中扩展RainLab.Blog 的插件。

我的插件的数据库出现在 SQL 数据库中,并且在编辑博客文章时采用新输入的后端表单呈现得很好,但是在尝试将更改保存到 SQL 时出现问题,它们显示为 NULL强>:

“方法 OleMol\BlogExtensions\Models\Extension::__toString() 不能抛出异常,捕获到 Illuminate\Database\Eloquent\JsonEncodingException: Error encoding model [OleMol\BlogExtensions\Models\Extension] with ID [2] to JSON: Recursion detected" on line 0 of C:\xampp\htdocs\vendor\laravel\framework\src\Illuminate\Support\Str.php

这似乎是一个Laravel 问题,这是我第一次在专业项目中使用十月。 最初,我尝试使用包含一堆列的表格,但为了排除故障,我将其精简为 一个 自定义列,直到找到答案为止。

我正在尝试跟随 this video tutorial,但扩展 RainLab.Blog 而不是 RainLab.User

cd C:\xampp\htdocs
php artisan create:plugin OleMol.BlogExtensions

插件\olemol\blogextensions\Plugin.php

<?php namespace OleMol\BlogExtensions;
                                                                                                   
use Backend;
use System\Classes\PluginBase;
use RainLab\Blog\Models\Post as PostModel;
use RainLab\Blog\Controllers\Posts as PostsController;
use OleMol\BlogExtensions\Models\Extension as ExtensionModel;
                                                                                                   
/**
 * BlogExtensions Plugin Information File
 */
class Plugin extends PluginBase    {
    /* RainLab.Blog is a dependency for this plugin. */
    public $require = [ 'RainLab.Blog' ];

    public function boot()    {
        PostModel::extend(function($model)  {
            $model -> hasOne[ 'blogextension' ] = [ 'OleMol\BlogExtensions\Models\Extension' ];
        });
        
        PostsController::extendFormFields(function($form, $model, $context) {
            //Only extend this controller if the model is a post.
            if (!$model instanceof PostModel)   { return; }
            
            //Don't create an extension to a model that does not exist.
            if (!$model -> exists)  { return; }
            
            //Ensure that the post always has an extension before adding fields.
            ExtensionModel::getFromPost($model);
            
            $form -> addTabFields([ 'blogextension[business_name ]' => [ 'label' => 'Business Name', 'tab' => 'Business' ]);
        });
    }                                                                                                
}                                                                                                    }

插件\olemol\blogextensions\updates\version.yaml

1.0.1:
- First version of BlogExtensions
1.0.2:
- create_business_table.php

plugins\olemol\blogextensions\updates\create_business_table.php

<?php namespace OleMol\BlogExtensions\Updates;
                                                                                                   
use Schema;
use October\Rain\Database\Schema\Blueprint;
use October\Rain\Database\Updates\Migration;
                                                                                                 
class CreateBusinessTable extends Migration    {
    public function up()    {
        Schema::create('olemol_blogextensions_business', function (Blueprint $table) {
            $table -> engine = 'InnoDB';
            $table -> increments('id');
            $table -> integer('post_id') -> unsigned() -> index();
            
            /*
             * This always ends up as NULL!
             * (Or default value in case it's not nullable).
             */
            $table -> string('business_name') -> nullable();
            
            $table -> timestamps();
        });
    }
                                                                                                
    public function down()    {
        Schema::dropIfExists('olemol_blogextensions_business');
    }
}

插件\olemol\blogextensions\models\Extension.php

<?php namespace OleMol\BlogExtensions\Models;
                                                                                                   
use Model;
                                                                                                   
/**
 * Extension Model
 */
class Extension extends Model    {
    use \October\Rain\Database\Traits\Validation;
    
    /**
     * @var string table associated with the model
     */
    public $table = 'olemol_blogextensions_business';
    
    /**
     * @var array dates attributes that should be mutated to dates
     */
    protected $dates = [ 'created_at', 'updated_at' ];

    public $belongsTo = [ 'post' => [ 'RainLab\Blog\Models\Post' ]];

    /* Helper-function to ensure that post always has an extension. */
    public static function getFromPost($post)   {
        if ($post -> extension) { return $post -> extension; }
        
        $extension = new static;
        $extension -> post = $post;
        $extension -> save();

        $post -> extension = $extension;
        
        return $extension;
    }
}

我已经包含了我认为必要的代码。这些文件仍然包含自动生成的空块,这些块被省略了。

php artisan plugin:refresh OleMol.BlogExtensions
php artisan project:sync
php artisan october:migrate

我的日程安排很紧,我觉得我在这一点上迷路了。 如果需要更多信息,请与我们联系。 如有任何帮助,我将不胜感激。

【问题讨论】:

    标签: php json laravel plugins octobercms


    【解决方案1】:

    我认为您的代码Extension.php-&gt; getFromPost method 需要一些更正

    // plugins\olemol\blogextensions\models\Extension.php: getFromPost method: 
    $post->extension = $extension;
    

    应该是:blogextension 而不是 only extension

    $post->blogextension = $extension;
    //     ^ HERE
    

    您需要在此处指定您的hasOne 关系名称

    如有疑问请留言

    【讨论】:

    • 你是救生员。
    猜你喜欢
    • 2018-11-26
    • 2016-05-18
    • 2017-07-27
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多