Php OctoberCMS:如何扩展System\Models\File并向其添加新字段?

Php OctoberCMS:如何扩展System\Models\File并向其添加新字段?,php,octobercms,octobercms-plugins,Php,Octobercms,Octobercms Plugins,我需要在system\u files表中添加一个额外的列。我不希望更改此表,因此我决定添加一个包含一列的新表,然后使用hasOne关系扩展System\Models\File模型。因此,我创建了一个名为myname\u plugin\u tag的新表,其中包含两个字段:file\u id和title,并为其创建了一个名为tag的新模型文件: class Tag extends Model { use \October\Rain\Database\Traits\Validation;

我需要在
system\u files
表中添加一个额外的列。我不希望更改此表,因此我决定添加一个包含一列的新表,然后使用
hasOne
关系扩展
System\Models\File
模型。因此,我创建了一个名为
myname\u plugin\u tag
的新表,其中包含两个字段:
file\u id
title
,并为其创建了一个名为
tag
的新模型文件:

class Tag extends Model
{
    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [
    ];

    /*
     * Disable timestamps by default.
     * Remove this line if timestamps are defined in the database table.
     */
    public $timestamps = false;

    /**
     * @var string The database table used by the model.
     */
    public $table = 'myname_plugin_tag';
}
在我的插件的
boot
方法中,我有:

System\Models\File::extend(function($model){
    $model->hasOne['tag']=[
        'MyName\Plugin\Models\Tag',
        'key' => 'file_id',
        'timestamps' => 'false'
    ];
});
这似乎就是我所要做的。但是,当使用我定义的新关系保存文件时,会出现错误

$user = Auth::getUser();

$tag = new Tag;
$tag->title = 'some tag';

$file = new File;
$file->data = Input::file('catalog');
$file->title = Input::get('title');
$file->tag = $tag; // => This line is the culprit. If I comment it, the exception will not raise

$user->files = $file;
$user->save();
例外情况:

调用未定义的方法October\Rain\Database\QueryBuilder::withTimestamps() E:\Development\git\catalog manager\vendor\laravel\framework\src\light\Database\Query\Builder.php第2123行

如果我删除行
'timestamps'=>'false'
,异常将得到修复,并且
$file
模型将被保存,但是
$tag
模型尚未保存

使用
$file->tag()->save($tag)
工作并保存标签!但问题并没有得到解决。我无法通过$user->files[0]->标记访问保存的属性,并出现以下错误:

正在尝试获取非对象的属性

{{dump($user->files[0])}
的输出清楚地表明,
标记
属性尚未添加到
文件
模型:

id  integer
disk_name  string
file_name  string
file_size  integer
content_type  string
title  string
description  NULL
field  string
sort_order  integer
created_at  string
updated_at  string
path  string
extension  string

因此,
文件
模型似乎不知道添加到它的新的
标记
属性。有什么我遗漏的吗?

你有没有尝试删除
“timestamps”=>“false”
?@OlegSamorai我删除了它,异常被修复。但是现在没有任何内容保存到
myname\u plugin\u标记中。仅保存
文件
模型。您是否已将belongsTo关系添加到MyName\Plugin\Models\Tag?@OlegSamorai我认为只有当我们要访问
标记
模型的
文件
时,才需要反向关系。顺便说一句,我在
标签
模型中添加了
项下的
关系,但仍然没有保存
标签
。插件启动()函数中是否执行了任何代码?您是否尝试删除
'timestamps'=>'false'
?@OlegSamorai我删除了它,异常已修复。但是现在没有任何内容保存到
myname\u plugin\u标记中。仅保存
文件
模型。您是否已将belongsTo关系添加到MyName\Plugin\Models\Tag?@OlegSamorai我认为只有当我们要访问
标记
模型的
文件
时,才需要反向关系。顺便说一下,我在
标签
模型中添加了
belongsTo
关系,但仍然没有保存
标签
。是否有代码执行到plugin boot()函数中?