We don't bite newbies here... much | |
PerlMonks |
Re: DBIx::Class and "complex" joins (was Re^4: DB: what kind of relationship?)by 1nickt (Canon) |
on May 03, 2020 at 20:13 UTC ( [id://11116406]=note: print w/replies, xml ) | Need Help?? |
AddendumI said above "it should be possible to do e.g. $painting->artist, but I think that could be solved with the existing DB schema using some more advanced DBIx features". You can add missing pseudo-relationships very easily by turning your ResultSet class into a Moo class and adding object methods. For this demonstration, add the following code to Painting.pm, Play.pm and Poem.pm (after the "do not add anything above this line" line!): ... and add the following to Artist.pm and Comment.pm:
Now in order to get "Poems not by English artists that tobyink doesn't hate", the query code remains unchanged (because the new accessors are DBIx-side, not SQL-side):
But the application code changes from: ... to:
You can use the new accessors throughout the application, e.g. for making a comparison similar to the one in "Comments by Users who speak the same Language as the Artist" in application code rather than SQL:
And you can add arbitrary methods to the classes, not just accessors. For example, if you are building a REST application where your DB tables and ORM classes represent resources exposed via API endpoints, you can add a custom chained deletion flow, or a standard method that builds the HTTP response object: ... so you can always call $obj->endpoint_data when you are finished processing the logic for the request. Hope this helps!
The way forward always starts with a minimal test.
In Section
Seekers of Perl Wisdom
|
|