1

我需要在表中添加一个额外的列system_files。我不想更改此表,因此我决定添加一个包含一列的新表,然后System\Models\File使用关系扩展模型hasOnemyname_plugin_tag因此,我创建了一个包含两个字段的新表:file_idtitle为其创建了一个新模型文件,名为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\Illuminate\Database\Query\Builder.php 行 2123

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

使用$file->tag()->save($tag)工作并保存标签!但问题并没有解决。我无法通过 $user->files[0]->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不知道tag添加到它的新属性。有什么我想念的吗?

4

0 回答 0