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

时间:2017-02-17 13:47:17

标签: php octobercms octobercms-plugins

我需要在sh script: """\ foo='bar' \ echo $foo \ """, returnStdout: true 表中添加一个额外的列。我不想更改此表,因此我决定添加一个包含一列的新表,然后使用system_files关系扩展System\Models\File模型。因此,我创建了一个名为hasOne的新表,其中包含两个字段:myname_plugin_tagfile_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'
    ];
});

例外:

  

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

如果我删除了行$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(); ,则会修复异常并保存'timestamps' => 'false'模型,但尚未保存$file模型。

使用$tag有效并保存标记!但问题没有解决。我无法通过$ user-> files [0] - >标记访问已保存的媒体资源并收到此错误:

  

尝试获取非对象的属性

$file->tag()->save($tag)的输出清楚地显示{{dump($user->files[0])}}属性尚未添加到tag模型中:

File

所以似乎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 模型不知道添加了新的File属性。有什么我想念的吗?

0 个答案:

没有答案