Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:25:45+00:00 2026-05-16T04:25:45+00:00

Problem definition. I have multiple clients with multiple users. Each client needs to be

  • 0

Problem definition.

I have multiple clients with multiple users. Each client needs to be able to associate custom data with a user, search, and order by.


Database Solution:

A table Customfields which defines the customfields table. It has an id and name.
It has a has_many relationship with a Userfields table (aka "attributes").

The Userfields table has a userid, customfieldid, content and id.
It belongs_to a Useraccounts table (aka "useraccount") and Customfields (aka "customfield")


Proposed select statement that I want:

This is a select statement that achieves and produces what I need.

SELECT ua.*, (
    SELECT content FROM Userfields uf
    INNER JOIN Customfields cf
        ON cf.id = uf.customfieldid
    WHERE cf.name = 'Mothers birthdate'
    AND uf.uid=ua.uid
) AS 'Mothers birthdate',
    (
    SELECT content FROM Userfields uf
    INNER JOIN Customfields cf
        ON cf.id = uf.customfieldid
    WHERE cf.name = 'Join Date' AND
    uf.uid=ua.uid
) AS 'Join Date'
FROM UserAccounts ua
ORDER BY 'Mothers birthdate';

In this case their could be anything from 0 … x sub SELECT statements in the select statement and any one of them or none of them could be wanting to be ordered by.


Question

How do I achieve this with a ->search on my DBIx-Class resultset or how do I achieve the same result with a ->search on my DBIx-Class resultset?

Here is how I usually select from my Useraccounts table, although I am unsure how to do the complex statement that I want to from here.

my @users = $db->resultset('Useraccounts')->search(
    undef,
    {
        page        => $page,
        join        => 'attributes',
        ...
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:25:46+00:00Added an answer on May 16, 2026 at 4:25 am

    This is really pretty hairy, and any solution isn’t going to be pretty, but it does look to be possible if you bend the rules a little bit. Forgive any mistakes I make, as I didn’t go and create a schema to test this on, but it’s based on the best info I have (and much help from ribasushi).

    First, (assuming that your userfields table has a belongs_to relation with the customfields table, called customfield)

    my $mbd = $userfields_rs->search(
        {
          'customfield.name' => 'Mothers birthdate',
          'uf.uid' => \'me.uid' # reference to outer query
        },
        {
          join => 'customfield',
          alias => 'uf', # don't shadow the 'me' alias here.
        }
    )->get_column('content')->as_query;
    
    # Subqueries and -as don't currently mix, so hack the SQL ourselves
    $mbd->[0] .= q{ AS 'Mothers Birthdate'};
    

    The literal me.uid that uf.uid is being matched against is an unbound variable — it’s the uid field from the query that we’re eventually going to put this query into as a subselect. By default DBIC aliases the table that the query is addressing to me; if you gave it a different alias then you would use something diferent here.
    Anyway, You could repeat this as_query business with as many different fields as you like, just varying the field-name (if you’re smart, you’ll write a method to generate them), and put them in an array, so now let’s suppose that @field_queries is an array, containing $mbd above as well as another one based on Join Date, and anything you like.

    Once you have that, it’s as “simple” as…

    my $users = $useraccounts_rs->search(
        { }, # any search criteria can go in here,
        {
          '+select' => [ @field_queries ],
          '+as' => [qw/mothers_birthdate join_date/], # this is not SQL AS
          order_by => {-asc => 'Mothers birthdate'},
        }
    );
    

    which will include each of the subqueries into the select.

    Now for the sad part: as of right now, this whole thing actually won’t work, because subqueries with placeholders don’t work properly. So for now you need an additional workaround: instead of 'customfield.name' => 'Mothers birthdate' in the subselect search, do 'customfield.name' => \q{'Mothers birthdate'} — this is using literal SQL for the field name (BE CAREFUL of SQL injection here!), which will sidestep the placeholder bug. But in the not-too-distant future, that bug will be resolved and the code above will work okay, and we’ll update the answer to let you know that’s the case.

    See DBIx::Class::ResultSource order_by documentation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 535k
  • Answers 535k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Move the idstafftable condition to the join instead of the… May 17, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer After a bit more searching, this appears to be a… May 17, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer For just handling clicks you could use click() $('a').click(callback); $('form').click(another_callback);… May 17, 2026 at 1:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Here are two roles: Trainer and Trainee. Trainer may have multiple trainees. While each
I have to use an external library, but am getting a multiple definition error
I have a weird problem with my i18n doctrine schema and i.a. the admin
I have a simple entity called Store built in Xcode's data modeler for use
I have a problem with this question from my professor. Here is the question:
I have got a problem on casting an object to one of it's base
I have an odd problem. I have a number of C# apps that utilize
I have a WCF service and methods are exposed as below: public interface IService
Background Currently I have a C# Silverlight business application which uses RIA Services. The
I have a WSDL that the consumer of my web service expects will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.