[Dbix-class] relationship with additional constraints?
Mitchell Elutovich
melutovich at gmail.com
Thu May 17 10:30:24 GMT 2018
The following (below) I sent to "Frew" upon which he updated the post to
indicate that the hack does not work. (Some of his hack does work and some
not.)
In his example is:
my @rows = do {
local $My::Schema::Result::Foo::SHARE_TYPE = [1, 2];
$rs->search(undef, { join => 'output_devices' })->all
};
This usage still works having the local and "all" in the same scope.
----
someone shared with me your
https://blog.afoolishmanifesto.com/posts/dbix-class-parameterized-
relationships/#extended-relationship-refresher
I ran into a problem with the wrapping into a resultset method
At the time the "all" is called the "local" has gone out of scope so
the die gets triggered
die "no share_type specified!" unless $SHARE_TYPE;
For myself I had to have this as the last in the chain and in the
resultset method call the all itself.
On Thu, May 17, 2018 at 12:35 PM, Andrew Beverley <andy at andybev.com> wrote:
> On Thu, 17 May 2018 11:12:45 +0200 dodds at united-domains.de wrote:
> > Is it possible to create a relationship which has, along with the
> > join condition, an additional constraint where the value could
> > somehow be passed when the search() method is called?
>
> This should answer your question:
>
> https://blog.afoolishmanifesto.com/posts/dbix-class-parameterized-
> relationships/
>
> Interestingly the post has been updated since I last looked at it
> saying that approach no longer works. I'm not sure why, as it still
> seems to work for me. Frew - can you elaborate?
>
> > - Get a list of all movies, as well as a review by Sue if it exists
> > - If a movie does not have a review by Sue then it should still be
> > returned with reviews.text = NULL.
> > - If a movie does not have a review by Sue but it has a review by
> > someone else then it should also be returned with reviews.text = NULL
>
> The other method you might want to consider is using a correlated
> sub-query. It's a little cleaner from a DBIx::Class viewpoint, but I
> think you'd need to pull out each review column separately, so would
> depend what you want to retrieve. Another excellent blog from Frew:
>
> https://blog.afoolishmanifesto.com/posts/introducing-dbix-class-helper-
> resultset-correlaterelationship/
>
> It allows you to do something like this (untested):
>
> '+columns' => {
> sue_review => $self->schema->resultset('Review')
> ->correlate('movies')
> ->search({ person => 'sue' })
> ->get_column('review_text')
> ->as_query,
> },
>
> Andy
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/
> dbix-class at lists.scsys.co.uk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20180517/c2044d17/attachment.htm>
More information about the DBIx-Class
mailing list