octobercms mysql,OctoberCMS Builder插件,上传文件并存储在数据库中

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

%7B%7B%20model_data.social_logo.getPath()%20%7D%7D

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 }}

%7B%7B%20current_model_data.social_logo.getPath()%20%7D%7D

{% endfor %}

Thanks for support guys .. hope this helps to someone who can be new to OctoberCMS.. highly appreciated ..

Thank you ..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值