Deal_with_data_objects_in_mojolicious
- The Mojolicious ResultSet Object
One thing which I strongly believe it helps to understand a particular projects, or even a particular piece of a system is the understanding of the data flow and what each component receives.
Something which can be taunting is to try to get the information passed to the templates of your Mojolicious project. Or what the controller sends.
The controllers usually leverage the database which in the frameworks like Mojolicious are called models.
Lets keep the flow in the mind. The user sends a request to open a page in the web app. The route maps to a specific function in the controller. Lets say list all books. The books lives in the db and the controller will make a query to get them, do something with them and pass them in the view, which knows how to render the html.
Anyway I think these are known to most of the readers. And likely most of them
knows already the Data::Dumper.
Then what usually happens, you see a reference which you want to investigate and you pass it to the Dumper; Super… and then you get something like
1 --- Base ResultSet Object ---
2 $VAR1 = bless( {
3 'result_source' => bless( { ... } ), # Internal object representing the 'User' table
4 'schema' => bless( { ... } ), # A reference to the main schema/DB connection
5 'attrs' => { # << The attributes of the query
6 # This is empty because we haven't added any conditions yet
7 },
8 'related_resultset' => undef
9 }, 'DBIx::Class::ResultSet::User' ); # << The class of the object
And that’s not something which you can easily debug, as it be really reall long.
- Thing to keep in mind The ResultSet object represents the container for the query parameters (see attrs)
And in additional
- It doesn’t hit the database when you create it. It only runs a query when you call a method that needs data (like ->next, ->all, ->first, or ->count).
- It’s Chainable: You can call methods on it to progressively build a more complex query (e.g., $rs->search(…)->update(…)).
- It’s a Query Representation: It contains the state of the query you’re building: the table, the WHERE conditions, the ORDER BY clauses, etc. Think of it as a container for the query parameters (what you see in the attrs)