I am new to OctoberCMS and i am trying to create one plugin using builder plugin itself which OctoberCMS provides OctoberCMS Builder Plugin called something like "Social Links", everything working fine expect i am unable to understand the logic of uploading file to any directory and storing that name to my database table's particular field. My table's field name is "social_logo" in which i am trying to store the file name which will be uploaded.
I am able to upload file to default directory at the moment whichever OctoberCMS generating with the file i am uploading. But the thing is i am unable to store that particular file name to my database table's field.
Can someone guide me what should i do to achieve this ?
Here is the my model file what i have done so far.
SocialLinks.php
use Model;
/**
* Model
*/
class Sociallink 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 = 'technobrave_sociallinks_';
public $attachOne = [
'social_logo' => 'System\Models\File'
];
}
Fields.yaml
fields:
social_logo:
label: 'technobrave.sociallinks::lang.Sociallink.social_logo'
span: auto
oc.commentPosition: ''
mode: file
useCaption: true
thumbOptions:
mode: crop
extension: auto
type: fileupload
columns.yaml
columns:
social_logo:
label: 'technobrave.sociallinks::lang.Sociallink.social_logo'
type: text
searchable: true
sortable: true
As you can see on above code, for now i have only 1 field because i am having issues with this particular field only while uploading an image, i want to store that file name . All other form attributes working for me like text,textarea etc so for now i m only trying to achieve this thing with this single field.
Can someone guide me what should i do to solve this ?
Thanks
解决方案
Ok guys, eventually i have solved this by following these steps.
First of all, we do not need to create any column in database table to store any file upload names in any field in database table as OctoberCMS will automatically in system_files table. So i simply removed 'social_logo' field via builder plugin.
Then i removed old file upload control from my current Social Links plugin using Builder plugin from admin and simply created new one without setting up any relation with any table's field.
Then simply i went to Sociallinks.php model,and made sure this code is there
public $attachOne = [
'social_logo' => 'System\Models\File'
];
Also make sure your mode should be image and your type can be fileupload as i am able to see images in edit record section like this .. you can update this code though based on your needs but this works for me well so i put this code...
fields.yaml
fields:
social_logo:
label: 'Social Logo'
mode: image
span: auto
type: fileupload
and then i went to admin area and created test template and in Code tab i put this code just to check single record for now,
use technobrave\sociallinks\Models\Sociallink;
function onStart()
{
$this['model_data'] = Sociallink::first();
}
and in template markup, i simply put this
and whoa, i can see image .. :)
Additionaly, if you want multiple data then you can put this code in Code Tab,
use technobrave\sociallinks\Models\Sociallink;
function onStart()
{
$this['model_data'] = Sociallink::all();
}
And your html markup tab will contain following code,
Image List
{% for current_model_data in model_data %}
{{ current_model_data.id }}
{% endfor %}
Thanks for support guys .. hope this helps to someone who can be new to OctoberCMS.. highly appreciated ..
Thank you ..