I need to add an extra column to the system_files
table. I'd prefer not to change this table, so I decided to add a new table with one column and then extend the System\Models\File
model with a hasOne
relationship. I therefore created a new table called myname_plugin_tag
with two fields: file_id
and title
and created a new model file for it called 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';
}
And in the boot
method of my plugin I have:
System\Models\File::extend(function($model){
$model->hasOne['tag']=[
'MyName\Plugin\Models\Tag',
'key' => 'file_id',
'timestamps' => 'false'
];
});
That seems to be all I have to do. But when saving a file with the new relationship I've defined, I get an error.
$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();
Exception:
Call to undefined method October\Rain\Database\QueryBuilder::withTimestamps() E:\Development\git\catalog-manager\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php line 2123
If I remove the line 'timestamps' => 'false'
, the exception gets fixed and $file
model will be saved, but $tag
model is not saved yet.
Using $file->tag()->save($tag)
works and saves the tag! But the problem is not solved. I cannot access the saved property via $user->files[0]->tag and get this error:
Trying to get property of non-object
The output of {{dump($user->files[0])}}
clearly shows that tag
property has not been added to the File
model:
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
So it seems that the File
model is not aware of the new tag
property added to it. Is there anything I'm missing?