使深层相关字段可在OctoberCMS中进行搜索 [英] Making a deep related field searchable in OctoberCMS
问题描述
我的Invoice
模型的columns.yaml
文件有以下列:
My my columns.yaml
file for the Invoice
model I have the following column:
transaction[user][email]:
label: Login email
type: text
searchable: true // doesn't work!
不幸的是,searchable
位不起作用,我知道这是因为我正在使用transaction[user][email]
而不是将relation
和select
作为列的一部分.
Unfortunately, the searchable
bit doesn't work and I understand that this is because I am using transaction[user][email]
instead of having a relation
and select
as part of the column.
我的问题是,当我要深入2个关系时,可以像这样使用relation
和select
吗?还是我需要其他方法?
My question is, can I somehow use relation
and select
when I'm going 2 relations deep, like this, or do I need a different approach?
我的Invoice
模型已定义以下关系:
My Invoice
model has this relation defined:
public $morphOne = [
'transaction' => ['Namespace\Plugin\Models\Transaction', 'name' => 'transactionable']
];
我的Transaction
模型定义了以下关系:
And my Transaction
model has this relation defined:
public $belongsTo = [
'user' => ['Rainlab\User\Models\User'],
];
因此,从本质上讲,我希望能够有发票的后端列表,其中用户的电子邮件地址显示在其中一列中,并且还可以在搜索框中输入电子邮件地址以进行过滤仅列出与具有该电子邮件地址的用户相关联的发票.
So, essentially, I want to be able have a backend list of invoices with the user's email address showing in one of the columns, and also make it possible for an email address to be entered in the search box in order to filter the list only for invoices associated with the user with that email address.
推荐答案
嗯,我在本地尝试了一个演示,它的运行情况还不错.
Hmm, I tried one demo in my local and its working just fine.
在您的Controller
中添加此代码we are going to extend query
以添加我们的custom field
,甚至可以是searchable
甚至是2 relation deeper
In Your Controller
add this code we are going to extend query
to add our custom field
which can be searchable
even its 2 relation deeper
<?php
use Rainlab\User\Models\User;
use Namespace\Plugin\Models\Invoice;
use Namespace\Plugin\Models\Transaction;
... your controller code ...
public function listExtendQuery($query)
{
$invoiceTableName = (new Invoice)->getTable();
$transactionTableName = (new Transaction)->getTable();
$userTableName = (new User)->getTable();
$query->addSelect($userTableName . '.email as email');
$query->leftJoin($transactionTableName, $invoiceTableName . '.id', '=', $transactionTableName . '.invoice_id');
$query->leftJoin($userTableName, $userTableName . '.id', '=', $transactionTableName . '.user_id');
}
...
,然后在您的columns.yaml
内添加
columns:
id:
label: id
type: number
invisible: false
... other columns
email:
label: user_email
type: text
default: none
searchable: true
select: email
现在您也可以search from email
,希望它能起作用
now you are able to search from email
as well, hope this will work
如果遇到任何错误,请发表评论.
if you get any error please comment.
这篇关于使深层相关字段可在OctoberCMS中进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!