Search code examples
perlcatalystdbix-class

DBIx::Class virtual column?


I am building an app with a DBIx::Class (Loader) based ORM. Most of my database models have a 'name' column. One of my controllers searches all schema classes using primarily the 'name' column. A couple of schema classes however don't have a 'name' column.

Is it possible in DBIx::Class to add a sort of 'virtual' column that uses another column instead:

$resultset('Account')->search({name => 'foobar'})

secretly rewrites to

$resultset('Account')->search({accountnumber => 'foobar'})

I hope I am making sense, anyone?

Thx, Rob


Solution

  • As far as I know this is not possible - not as a key in a search query anyhow. What you could do is create a base-class for all your resultset classes (you are using load_namespaces, right?) which has a method find_by_name or similar, that performs this search on the correct column. The column could default to name but be overridden by a class attribute - which you can set up with mk_accessor.

    You can set this base-class to be the default resultset class for all your resultsets with the default_resultset_class attribute of load_namespaces