OctoberCMS - 在后端访问模型数据...返回null?

时间:2017-06-20 17:22:22

标签: php laravel file-upload octobercms octobercms-backend

我遇到的问题是,我无法访问文件模型中的某些信息,例如文件路径,文件名和system_files表中存储的其他信息。我相信我已经确定了问题的来源,但我不确定如何解决它。下面我将展示一些代码。

1)File.php〜文件模型

<?php namespace Compuflex\Downloads\Models;
use Model;
use October\Rain\Database\Attach\File as FileBase;
/**
 * Model
 */
class File extends FileBase
{
    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 = 'compuflex_downloads_files';
    public $attachOne = [
            'file' => ['System\Models\File', 
                'key' => 'id']
    ];

    public $belongsToMany = [
            'user' => ['RainLab\User\Models\User']
    ];
}

在此文件中,您可以看到标识了两个关系:$attachOne将文件上载附加到文件模型以及$belongsToMany,它标识用户和用户之间的多对多关系。文件。 (许多文件可以属于许多用户,或者一个用户可以拥有多个文件,但一个文件也可以属于许多用户)。这是设置fileuser关系的文件,您将在columns.yaml文件中看到该关系。只是旁注,我设置'key' =&gt; 'id'的唯一原因是确保它实际上将'key'标识为'id'

2)列和字段

我不认为fields.yaml文件是必要的,但为了以防万一,你可以在这里找到它: fields.yaml

columns.yaml

columns:
    file_name:
        label: 'compuflex.downloads::lang.file.name'
        type: text
        searchable: true
        sortable: true
    full_name:
        label: 'compuflex.downloads::lang.file.username'
        type: text
        searchable: true
        sortable: true
        select: name
        relation: user
    email:
        label: 'compuflex.downloads::lang.file.email'
        type: text
        searchable: true
        sortable: true
        select: email
        relation: user
    file:
        label: 'compuflex.downloads::lang.file.path'
        type: text
        searchable: true
        sortable: true
        select: file_name
        relation: file

我更关心columns.yaml文件,因为我正在尝试修复的是后端控制器,它显示文件信息列表以及有关他们“附加”的用户的信息“ 至。

以下是视图的截图: Screenshot of Back-end Controller 如您所见,“文件”选项卡未显示有关该文件的任何信息,它应该显示file_name表中的system_files列或至少来自文件模型,但是它什么也没显示。

现在,我觉得有趣的是,如果我将columns.yaml文件中的最后一个条目从原始文件更改为:

file:
    label: 'compuflex.downloads::lang.file.path'
    type: text
    searchable: true
    sortable: true

然后在后端控制器中输出以下内容:

After the changes...

因此,正如您所看到的那样,信息 IS 涉嫌),但我只是不确定如何正确访问它。

所以,我做了最后一次测试,我会告诉你,我知道如果select:文件中的columns.yaml属性设置为不在{{{ 1}}表应该产生某种类型的SQL错误(例如找不到列名)。我是对的...所以,我将其设置为system_files而不是'ile_name',仅用于测试目的。

以下是错误消息:

file_name

现在,为了更明确地指出这个问题,我认为问题可能来自:

SQLSTATE[HY000]: General error: 1 no such column: ile_name (SQL: select "compuflex_downloads_files".*, (select group_concat(name, ', ') 
from "users" inner join "file_user" on "users"."id" = "file_user"."user_id" where "file_user"."file_id" = 
"compuflex_downloads_files"."id") as "full_name", (select group_concat(email, ', ') from "users" inner join "file_user" on "users"."id" = 
"file_user"."user_id" where "file_user"."file_id" = "compuflex_downloads_files"."id") as "email", (select ile_name from "system_files" 
where "system_files"."attachment_id" = "compuflex_downloads_files"."id" and "system_files"."attachment_type" = ? and "field" = ?) as 
"file" from "compuflex_downloads_files" order by "file_name" desc)

我认为那些(select ile_name from "system_files" where "system_files"."attachment_id" = "compuflex_downloads_files"."id" and "system_files"."attachment_type" = ? and "field" = ?) 导致此查询从表中返回?结果,从而导致没有输出。我不是百分之百,这只是一种可能性。问题是,我不知道OctoberCMS在哪里构建这些查询,或者是否有任何简单的方法来修复它而不触及10月的实际代码(原因很明显)。

只是一个注释: 如果我在这里收到太多信息,我道歉,但我试图将其删除。我只想告诉你我 DID 尝试自行排除故障并解决这个问题,但无济于事。

1 个答案:

答案 0 :(得分:0)

你太近了,你只需要将type: text改为type: relation,你也必须设置nameFrom属性:

file:
    label: 'compuflex.downloads::lang.file.path'
    type: relation
    searchable: true
    sortable: true
    nameFrom: file_name
    relation: file