问题
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
<?php namespace Technobrave\SocialLinks\Models;
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
回答1:
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
<img src="{{ model_data.social_logo.getPath() }}" />
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,
<h2>Image List</h2>
<ul>
{% for current_model_data in model_data %}
<h3>{{ current_model_data.id }}</h3>
<img src="{{ current_model_data.social_logo.getPath() }}" />
{% endfor %}
</ul>
Thanks for support guys .. hope this helps to someone who can be new to OctoberCMS.. highly appreciated ..
Thank you ..
回答2:
If it is necessary for you to have the file name stored in the database, the best way might be to use the Media Finder Widget instead of the File Upload Widget, and remove the attachOne relationship from your model.
The MediaFinder widget lets you store files anywhere within the
storage/media
subdirectory of your OctoberCMS installation, choosing the exact storage path therein that you want, and simply returns the stored file path as a string - that you then need to store as a column in the database record for your model, just as you seem to have had in mind.The File upload widget, combined with the attachOne (or attachMany) relationship works much differently. The attachOne (or attachMany) relationship does not correspond to an actual database column for your model. The information which files are attached to what other models here or there is stored and retrieved elsewhere - how exactly, I don't know. (The information about uploaded files through the File upload widget and their storage paths (which are generated randomly with no user control) are stored in the
system_files
database table - how the association is made to the parent models where the files are attached, I have no clue.)
You already have a file name column in your database table, and it seems that you would like your file names to be somehow "nice" and readable and perhaps under user control. So I would suggest that you simply change your fields.yaml like so:
fields:
social_logo:
label: 'technobrave.sociallinks::lang.Sociallink.social_logo'
span: auto
oc.commentPosition: ''
type: mediafinder
thumbOptions:
mode: crop
extension: auto
(I don't know if the thumbOptions
settings work here. You can try and see. As for the useCaption
it can not be used here, since files in the media library are simply referenced by strings and have no corresponding eloquent model with additional information like title
and description
, as the File Upload files do.)
来源:https://stackoverflow.com/questions/40174178/octobercms-builder-plugin-uploading-file-and-storing-in-database