十月 CMS - 自定义插件:JSON 检测到递归
October CMS - Custom Plugin: JSON Recursion Detected
我想开发一个扩展 RainLab.Blog in October CMS 的插件。
我的插件数据库出现在 SQL 数据库和后端表单中,在编辑博客时采用新输入 post 呈现得很好,但在尝试将更改保存到 SQL,它们出现的地方 NULL:
"Method OleMol\BlogExtensions\Models\Extension::__toString() must not throw an exception, caught 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 问题,这是我第一次在专业项目中使用 October。
最初,我尝试将 table 与一堆列一起使用,但为了进行故障排除,我将其简化为 一个 自定义列,直到找到答案。
我正在尝试跟随 this video tutorial,但扩展 RainLab.Blog 而不是 RainLab.User.
cd C:\xampp\htdocs
php artisan create:plugin OleMol.BlogExtensions
plugins\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' ]);
});
}
} }
plugins\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');
}
}
plugins\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
我的日程很紧,此时我觉得自己迷路了。
如果需要更多信息,请与我们联系。
我将不胜感激任何帮助。
我认为您的代码 Extension.php-> getFromPost method
需要一些更正
// plugins\olemol\blogextensions\models\Extension.php: getFromPost method:
$post->extension = $extension;
应该是:blogextension
而不是only extension
$post->blogextension = $extension;
// ^ HERE
您需要在此处指定您的 hasOne
关系名称
如有疑问请评论
我想开发一个扩展 RainLab.Blog in October CMS 的插件。
我的插件数据库出现在 SQL 数据库和后端表单中,在编辑博客时采用新输入 post 呈现得很好,但在尝试将更改保存到 SQL,它们出现的地方 NULL:
"Method OleMol\BlogExtensions\Models\Extension::__toString() must not throw an exception, caught 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 问题,这是我第一次在专业项目中使用 October。 最初,我尝试将 table 与一堆列一起使用,但为了进行故障排除,我将其简化为 一个 自定义列,直到找到答案。
我正在尝试跟随 this video tutorial,但扩展 RainLab.Blog 而不是 RainLab.User.
cd C:\xampp\htdocs
php artisan create:plugin OleMol.BlogExtensions
plugins\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' ]);
});
}
} }
plugins\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');
}
}
plugins\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
我的日程很紧,此时我觉得自己迷路了。 如果需要更多信息,请与我们联系。 我将不胜感激任何帮助。
我认为您的代码 Extension.php-> getFromPost method
需要一些更正
// plugins\olemol\blogextensions\models\Extension.php: getFromPost method:
$post->extension = $extension;
应该是:blogextension
而不是only extension
$post->blogextension = $extension;
// ^ HERE
您需要在此处指定您的 hasOne
关系名称
如有疑问请评论