Given two tables: (incoming greatly reduced/simplified example that exhibits the key problem)
app_data represents applications that can be subscribed to
id app_name
1 apple
2 berry
3 cherry
app_sub maps email addresses to applications
id email
1 alex
2 bob
2 coby
I’d like to generate a table from a single-user point-of-view showing which applications the current user is subscribed to and not.
For example, from Alex’s perspective I’d like to get:
desired_table
id app_name is_subscribed
1 apple true
2 berry false
3 cherry false
The following pure SQL query seems to be fine:
select id, app_name, email
from app_data left join ( select *
from app_sub
where email='alex'
) as subquery
on app_name.id=app_data.id;
However I’m having great difficulty getting that to work in dbix-class.
Alternatively, I tried to eliminate the subquery like so:
$app_data_resultset->search( { -or => [ { email => 'alex' },
{ email => undef },
],
},
{ select => [ qw{ me.id
me.app_name
app_sub.email
},
],
as => [ qw{ id
app_name
email
},
],
join => 'app_sub',
);
However, this (now expectedly) results in the following (after treating both 0 and null as false):
bad_table
id app_name is_subscribed
1 apple true
3 cherry false
Since ‘bob’ and ‘coby’ are subscribed to id 2, the where clause completely eliminates the second id.
Any help would be greatly appreciated!
Vincent, I am trying to join to a subquery now myself. I cannot find anyone succeeding so far.
An alternative is to just do it in plain SQL with $c->model(‘blah’)->storage->dbh->prepare(“query”) OR the ‘Arbitrary SQL through a custom ResultSource’ section of the DBIx Class Cookbook, which also seems like a reasonable way.