OctoberCMS Builder 插件,上传文件并存入数据库

标签 octobercms

我是 OctoberCMS 的新手,我正在尝试使用 builder 插件 本身创建一个插件,OctoberCMS 提供 OctoberCMS Builder Plugin称为“社交链接”之类的东西,一切正常,但我无法理解将文件上传到任何目录并将该名称存储到我的数据库表的特定字段的逻辑。我的表的字段名称是“social_logo”,我试图在其中存储将要上传的文件名。

我现在可以将文件上传到默认目录,无论 OctoberCMS 与我上传的文件一起生成。但问题是我无法将该特定文件名存储到我的数据库表的字段中。

有人可以指导我应该怎么做才能实现这一目标吗?

这是我到目前为止所做的模型文件。

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 个字段,因为我仅在上传图像时遇到此特定字段的问题,我想存储该文件名。所有其他对我有用的表单属性,如文本、文本区域等,所以现在我只试图用这个单一字段来实现这一点。

有人可以指导我如何解决这个问题吗?

谢谢

最佳答案

好的伙计们,最终我按照这些步骤解决了这个问题。

首先,我们不需要在数据库表中创建任何列来存储数据库表中任何字段中的任何文件上传名称,因为OctoberCMS会自动在< strong>system_files 表。所以我只是通过 builder 插件删除了“social_logo”字段。

然后我使用 admin 中的 Builder 插件 从我当前的 Social Links 插件 中删除了旧的文件上传 控件,并且简单地创建一个新的,而不与任何表的字段建立任何关系。

然后我简单地转到Sociallinks.php 模型,并确保这段代码在那里

public $attachOne = [
            'social_logo' => 'System\Models\File'
    ];

还要确保您的模式应该是图像并且您的类型 可以是 fileupload 因为我能够像这样在编辑记录部分看到图像..你可以根据你的需要更新这段代码,但这对我来说很好,所以我把这段代码...

fields.yaml

fields:
    social_logo:
        label: 'Social Logo'
        mode: image
        span: auto
        type: fileupload

然后我去了管理区并创建了测试模板,在代码选项卡中我把这段代码放在现在只是为了检查单个记录,

use technobrave\sociallinks\Models\Sociallink;


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

在模板标记中,我简单地把这个

<img src="{{ model_data.social_logo.getPath() }}" />

哇哦,我能看到图片了.. :)

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();
}

并且您的 html 标记选项卡将包含以下代码,

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

感谢大家的支持..希望这对 OctoberCMS 的新手有所帮助..非常感谢..

谢谢你..

关于OctoberCMS Builder 插件,上传文件并存入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40174178/

相关文章:

php - OctoberCMS 迁移表

php - Eloquent ORM - 模型关系

OctoberCMS Builder 插件后端下拉菜单

javascript - 如何在 octobercms 中使用 ajax 或 javascript 动态更改表单的方向

eloquent - OctoberCMS 范围不适用

php - 如何在OctoberCMS中禁用开发时的 Assets 合并

octobercms - afterSave() 尝试在 octoberCMS 中获取非对象的属性时出错

symfony - 如何检查今天是否在嫩枝中的两个日期之间?

php - Backend\Controllers\Users 必须定义属性 $relationConfig

email - OctoberCMS:如何在 Rainlab.User 插件中通过电子邮件设置用户帐户激活?