我遇到的问题是,我无法从文件模型中访问某些信息,例如文件路径、文件名和存储在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
标识用户和文件之间的多对多关系。(多个文件可以属于多个用户,或者一个用户可以拥有多个文件,但一个文件也可以属于多个用户)。这是设置您将在文件中看到的关系的file
文件。顺便说一句,我设置=>的唯一原因是确保它实际上是在识别as 。user
columns.yaml
'key'
'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
文件,因为我要修复的是后端控制器,它显示文件信息列表以及有关它们“附加”到的用户的信息。
这是视图的截图:
正如您所看到的,“文件”选项卡没有显示有关文件的任何信息,它应该显示表中的file_name
列,system_files
或者至少显示文件模型中的列,但它什么也没显示。
现在,我发现有趣的是,如果我将columns.yaml
文件中的最后一个条目从原始条目更改为:
file:
label: 'compuflex.downloads::lang.file.path'
type: text
searchable: true
sortable: true
然后在后端控制器中输出以下内容:
因此,如您所见,信息就在那里(据称),但我只是不确定如何正确访问它。
所以,我做了最后一个测试,我会告诉你,我知道如果文件中的select:
属性columns.yaml
设置为不在system_files
表中的列名,它应该会产生某种 SQL 错误(例如列名未找到)。我是对的......所以,我将它设置为,'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" = ?)
我认为这些?
导致此查询从表中返回NULL
结果,从而导致没有输出。我不是100%的,这只是一种可能性。问题是,我不知道 OctoberCMS 在哪里构建这些查询,或者是否有任何简单的方法可以在不触及 10 月份的实际代码的情况下解决这个问题(原因很明显)。
请注意:如果我在此处包含太多信息,我深表歉意,但我已尝试将其减少。我只是想告诉你我确实尝试过自己解决这个问题,但无济于事。