[英]perl DBIx::Class have a default filter for all searches on a table
I have a bunch of DBIx::Class Result classes created from my database schema by dbicdump / DBIx::Class::Schema::Loader 我有一堆DBIx :: Class结果类,由dbicdump / DBIx :: Class :: Schema :: Loader从我的数据库模式创建
I need to add an is_deleted
boolean column to one of the tables, and to filter out deleted records in all the existing searches and JOINs. 我需要将is_deleted
布尔列添加到其中一个表中,并在所有现有搜索和JOIN中过滤掉已删除的记录。
Unfortunately there are 30 or 40 places in the sprawling perl app which directly use the table in question, and at least the same number of places which JOIN to it via prefetch
or join
attributes to search()
. 不幸的是,在庞大的perl应用程序中有30或40个位置直接使用有问题的表,并且至少有与通过prefetch
或join
属性到search()
联接到该表的位置相同的位置。 Changing them all manually is possible, but very time consuming. 可以手动更改它们,但非常耗时。
Is there a way to add a default WHERE
clause to all queries which SELECT
from or JOIN
to a particular table? 有没有一种方法可以向所有SELECT
from或JOIN
到特定表的查询添加默认的WHERE
子句?
I'm after some way to be able to call resultset('MyTable')->search({},{})
and have WHERE is_deleted = 0
added to all the queries. 我正在寻找一种方法来调用resultset('MyTable')->search({},{})
并将WHERE is_deleted = 0
添加到所有查询中。 Plus have the same is_deleted = 0
filter applied when prefetch => [qw(my_table)],
is used. 另外,使用prefetch => [qw(my_table)],
将应用相同的is_deleted = 0
过滤器。
Yes, you can subclass your resultset class and override the search()
method to add your search criteria. 是的,您可以对结果集类进行子类化,并覆盖search()
方法以添加搜索条件。
package MyApp::Schema::Resultset::MyTable;
use strict;
use warnings;
use base 'DBIx::Class::Resultset';
sub search {
my $self = shift;
my ($cols, $opts) = @_;
$cols //= {};
$cols->{is_deleted} //= 0;
return $self->next::method($cols, $opts);
}
1;
Note: 注意:
is_deleted
if it's not already set - this allows us to look for rows with is_deleted
set to 1 if we ever need to do that. 我们仅将is_deleted
设置is_deleted
尚未设置的状态-如果需要的话,这允许我们查找将is_deleted
设置为1的行。 next::method()
instead of SUPER::method()
to call the superclass method. DBIx :: Class使用Class :: C3进行方法解析,因此我们使用next::method()
而不是SUPER::method()
来调用超类方法。 我将创建一个虚拟视图,并在表之外向该视图添加关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.