简体   繁体   English

使用名称而不是用户名的数据表查询

[英]Datatable query using name rather than username

I have a project where we don't store the name of a user and just use their username for everything. 我有一个项目,我们不存储用户名,仅将用户名用于所有内容。

I cam across a package called Ticketit which is a helpdesk ticketing system for Laravel. 我浏览了一个名为Ticketit的软件包,这是Laravel的帮助台票务系统。

The package uses the name of users for everything and so this caused a few errors. 该软件包将用户名用于所有内容,因此会导致一些错误。 I have a getNameAttribute() accessor on my User model and so for the most part this satisfies the package, however there were some places that explicitly called name in Eloquent ::lists() queries. 我的User模型上有一个getNameAttribute()访问器,因此在大多数情况下都可以满足该程序包的要求,但是在Eloquent ::lists()查询中有一些地方明确地调用了name

For these I manually replaced name with username inside my own fork of this repo , and have this linked up to my project. 对于这些,我手动在此仓库的自己的fork中用username替换了name ,并将其链接到我的项目。

The datatable loads as expected on the page, but when I try to sort by any of the other columns or run a search inside it I get 500 errors in my Network tab of developer tools. 数据表将按预期加载,但在尝试按其他任何列进行排序或在其中进行搜索时,开发人员工具的“网络”标签中出现500个错误。

Previewing the response shows this: 预览响应将显示以下内容:

QueryException in Connection.php line 662: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.name' in 'where clause' (SQL: select count(*) as aggregate from (select '1' as row_count from ticketit inner join users on users . id = ticketit . user_id inner join ticketit_statuses on ticketit_statuses . id = ticketit . status_id inner join ticketit_priorities on ticketit_priorities . id = ticketit . priority_id inner join ticketit_categories on ticketit_categories . id = ticketit . category_id where completed_at is null and (LOWER( ticketit . id ) LIKE %%h%% or LOWER( subject ) LIKE %%h%% or LOWER( ticketit_statuses . name ) LIKE %%h%% or LOWER( ticketit . updated_at ) LIKE %%h%% or LOWER( users . name ) LIKE %%h%% or LOWER( ticketit_priorities . name ) LIKE %%h%% or LOWER( users . name ) LIKE %%h%% or LOWER( ticketit_categories . name ) LIKE %%h%%)) count_row_table) Connection.php第662行中的QueryException:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ users.name”(SQL:从集合中选择count(*)作为汇总(从ticketit选择“ 1”作为row_count内部联接usersusersid = ticketituser_id内连接ticketit_statusesticketit_statusesid = ticketitstatus_id内部联接ticketit_prioritiesticketit_prioritiesid = ticketitpriority_id内部联接ticketit_categoriesticketit_categoriesid = ticketitcategory_id其中completed_at为空和( LOWER( ticketitid )LIKE %%ħ%%或LOWER( subject )LIKE %%ħ%%或LOWER( ticketit_statusesname )LIKE %%ħ%%或LOWER( ticketitupdated_at )LIKE %%ħ%%或LOWER( usersname )LIKE %%ħ%%或LOWER( ticketit_prioritiesname )LIKE %%ħ%%或LOWER( usersname )LIKE %%ħ%%或LOWER( ticketit_categoriesname )LIKE %% H% %))count_row_table)

Following the route this posts to, I get to TicketController@data . 按照此发布的路线,我到达TicketController@data In the original package this is: 在原始包装中,这是:

public function data(Datatables $datatables, $complete = false)
    {
        $user = $this->agent->find(auth()->user()->id);
        if ($user->isAdmin()) {
            if ($complete) {
                $collection = Ticket::complete();
            } else {
                $collection = Ticket::active();
            }
        } elseif ($user->isAgent()) {
            if ($complete) {
                $collection = Ticket::complete()->agentUserTickets($user->id);
            } else {
                $collection = Ticket::active()->agentUserTickets($user->id);
            }
        } else {
            if ($complete) {
                $collection = Ticket::userTickets($user->id)->complete();
            } else {
                $collection = Ticket::userTickets($user->id)->active();
            }
        }
        $collection
            ->join('users', 'users.id', '=', 'ticketit.user_id')
            ->join('ticketit_statuses', 'ticketit_statuses.id', '=', 'ticketit.status_id')
            ->join('ticketit_priorities', 'ticketit_priorities.id', '=', 'ticketit.priority_id')
            ->join('ticketit_categories', 'ticketit_categories.id', '=', 'ticketit.category_id')
            ->select([
                'ticketit.id',
                'ticketit.subject AS subject',
                'ticketit_statuses.name AS status',
                'ticketit_statuses.color AS color_status',
                'ticketit_priorities.color AS color_priority',
                'ticketit_categories.color AS color_category',
                'ticketit.id AS agent',
                'ticketit.updated_at AS updated_at',
                'ticketit_priorities.name AS priority',
                'users.name AS owner',
                'ticketit.agent_id',
                'ticketit_categories.name AS category',
            ]);
        $collection = $datatables->of($collection);
        $this->renderTicketTable($collection);
        $collection->editColumn('updated_at', '{!! \Carbon\Carbon::createFromFormat("Y-m-d H:i:s", $updated_at)->diffForHumans() !!}');
        return $collection->make(true);
    }

Which I have edited to this in my fork: 我已经在分叉中对此进行了编辑:

public function data(Datatables $datatables, $complete = false)
    {
        $user = $this->agent->find(auth()->user()->id);
        if ($user->isAdmin()) {
            if ($complete) {
                $collection = Ticket::complete();
            } else {
                $collection = Ticket::active();
            }
        } elseif ($user->isAgent()) {
            if ($complete) {
                $collection = Ticket::complete()->agentUserTickets($user->id);
            } else {
                $collection = Ticket::active()->agentUserTickets($user->id);
            }
        } else {
            if ($complete) {
                $collection = Ticket::userTickets($user->id)->complete();
            } else {
                $collection = Ticket::userTickets($user->id)->active();
            }
        }
        $collection
            ->join('users', 'users.id', '=', 'ticketit.user_id')
            ->join('ticketit_statuses', 'ticketit_statuses.id', '=', 'ticketit.status_id')
            ->join('ticketit_priorities', 'ticketit_priorities.id', '=', 'ticketit.priority_id')
            ->join('ticketit_categories', 'ticketit_categories.id', '=', 'ticketit.category_id')
            ->select([
                'ticketit.id',
                'ticketit.subject AS subject',
                'ticketit_statuses.name AS status',
                'ticketit_statuses.color AS color_status',
                'ticketit_priorities.color AS color_priority',
                'ticketit_categories.color AS color_category',
                'ticketit.id AS agent',
                'ticketit.updated_at AS updated_at',
                'ticketit_priorities.name AS priority',
                'users.username AS owner',
                'ticketit.agent_id',
                'ticketit_categories.name AS category',
            ]);
        $collection = $datatables->of($collection);
        $this->renderTicketTable($collection);
        $collection->editColumn('updated_at', '{!! \Carbon\Carbon::createFromFormat("Y-m-d H:i:s", $updated_at)->diffForHumans() !!}');
        return $collection->make(true);
    }

With this I have changed the users.name to users.username , but this isn't fixing the issue for me. 有了这个,我已经将users.name更改为users.username ,但这并不能解决我的问题。

Can anyone help me figure out why, or what else I need to change as I haven't had any luck figuring out where else I need to change this. 谁能帮我找出原因,或者我需要更改什么,因为我没有运气找出需要在其他地方更改的地方。

You'll drive yourself nuts trying to edit all the places where the 3rd-party code tries to access name . 您将大惊小怪,尝试编辑第三方代码尝试访问name My suggestion is to fix at the root rather than patch in many places: 我的建议是从根本上解决而不是在许多地方打补丁:

  1. Create a new name column in users table users表中创建一个新的name
  2. Fill that column with the values in the username column 在该列中填写username名列中的值

eg queries: 例如查询:

ALTER TABLE `users` ADD COLUMN `name` VARCHAR(30) NOT NULL AFTER `username`; 
UPDATE `users` SET `name` = `username`;

Now your DB will have the schema that your plugin expects. 现在,您的数据库将具有您的插件期望的架构。

I found the issue, it was in the initialisation of the DataTable where it was calling users.name rather than users.username . 我发现了问题所在,这是在初始化DataTable时调用了users.name而不是users.username

After updating this, clearing the views cache php artisan view:clear it all worked fine! 更新此内容后,清除视图缓存php artisan view:clear一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM