OctoberCMS Builder插件,上传文件并存储在数据库中 [英] OctoberCMS Builder plugin, uploading file and storing in database

查看:204
本文介绍了OctoberCMS Builder插件,上传文件并存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OctoberCMS 的新手,我正尝试使用 OctoberCMS 提供社交链接",一切正常,我无法理解将文件上传到任何目录并将该名称存储到数据库表的特定字段中的逻辑.我表的字段名称是" social_logo ",我试图在其中存储将要上传的文件名.

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.

无论 OctoberCMS 是使用我上传的文件生成的那一刻,我都可以将文件上传到默认目录.但问题是我无法将该特定文件名存储到数据库表的字段中.

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

您可以在上面的代码中看到,目前我只有1个字段,因为我仅在上载图像时遇到该特定字段的问题,我想存储该文件名.所有其他适用于我的表单属性(例如文本,textarea等),所以现在我仅尝试通过单个字段来实现.

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 ?

谢谢

推荐答案

好的,最终我已经按照以下步骤解决了这个问题.

Ok guys, eventually i have solved this by following these steps.

首先,我们不需要在数据库表中创建任何列即可在数据库表的任何字段中存储任何文件上载名称,因为 OctoberCMS 将自动在 system_files 表.因此,我只是通过 builder 插件删除了" social_logo "字段.

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.

然后我使用 admin 中的 Builder插件从我当前的 Social Links插件中删除了旧的文件上传控件,然后只需创建一个新表,而无需与任何表的字段建立任何关系.

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.

然后我只是进入了 Sociallinks.php 模型,并确保此代码在那里

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 .. :)

此外,如果您需要多个数据,则可以将此代码放在代码"标签中,

use technobrave\sociallinks\Models\Sociallink;


function onStart()
{
    $this['model_data'] = Sociallink::all();
}

并且您的html标记标签将包含以下代码,

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>

感谢支持人员..希望这对十月CMS的新手有所帮助..高度赞赏..

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

谢谢..

这篇关于OctoberCMS Builder插件,上传文件并存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆