使深层相关字段可在OctoberCMS中进行搜索 [英] Making a deep related field searchable in OctoberCMS

查看:83
本文介绍了使深层相关字段可在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]而不是将relationselect作为列的一部分.

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个关系时,可以像这样使用relationselect吗?还是我需要其他方法?

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

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