<div dir="ltr">What we're doing to subclass is <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">basically this (</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">
obviously we're </div>using Moose):<br><br><span style="font-family:courier new,monospace">around inflate_result => sub {<br> my $orig = shift;<br> my $self = shift;<br><br> my $row = $self->$orig( @_ );<br>
<br> bless $row, __PACKAGE__ if ref( $row ) ne __PACKAGE__;<br><br> my $product_subclass;<br> foreach ( $row ) {<br> if ( $_->is_of_type_one ) { $product_subclass = 'One'; }<br> elsif ( $_->is_of_type_two ) { $product_subclass = 'Two'; }<br>
elsif ( $_->is_of_type_three ) { $product_subclass = 'Three'; }<br> }<br><br> if ( defined $product_subclass ) {<br> $product_subclass = __PACKAGE__.'::'.$product_subclass;<br> $self->ensure_class_loaded( $product_subclass );<br>
bless $row, $product_subclass;<br> }<br><br> return $row;<br>};<span class="sewcm2fmlef7j9m"></span><span class="sewcm2fmlef7j9m"></span></span><br><br>So if this is in Result/MySubclassableTable.pm, that would rebless to Result/MySubclassableTable/One.pm and <div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">
its siblings.<br></div><br><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline">The initial bless is there because we saw some rows being returned as a subclassed object already (never truly understood why, but probably interpreter persistence has to do with it) which broke things.<br>
</div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 28 May 2014 23:43, Mitchell Elutovich <span dir="ltr"><<a href="mailto:melutovich@gmail.com" target="_blank">melutovich@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><font color="#000000" face="Times New Roman" size="3"><b>Louis Erickson:</b></font><div class=""><br><div>
<b style="color:rgb(0,0,0);font-family:'Times New Roman';font-size:medium">></b><span style="color:rgb(0,0,0)">You mean like DBIx::Class::DynamicSubclass? The only odd part is the union of sql select, which sounds like a view to me.</span></div>
</div><div><span style="color:rgb(0,0,0)">I'll have to think about the possibility of just using a view instead of a resultsource.</span></div><div><span style="color:rgb(0,0,0)"><br></span></div><div><span style="color:rgb(0,0,0)">Yes I've used DynamicSubclass in which it is described (in the </span><font color="#000000">Cookbook.pod#Dynamic_Sub-classing_DBIx::Class_proxy_classes) </font><span style="color:rgb(0,0,0)">as:</span></div>
<div><span style="color:rgb(0,0,0)"><br></span></div><div><div><font color="#000000">|DBIx::Class classes are proxy classes, therefore some different techniques need to be employed for more than basic subclassing. In this example we have a single user table that carries a boolean bit for |admin. We would like to give the admin users objects (DBIx::Class::Row) the same methods as a regular user but also special admin only methods. It doesn't make sense to create two separate proxy-class |files for this. We would be copying all the user methods into the Admin class. There is a cleaner way to accomplish this.</font></div>
<div><font color="#000000">|</font></div><div><font color="#000000">|Overriding the inflate_result method within the User proxy-class gives us the effect we want. This method is called by DBIx::Class::ResultSet when inflating a result from storage. So we grab the object being |returned, inspect the values we are looking for, bless it if it's an admin object, and then return it.</font></div>
</div><div><font color="#000000"><br></font></div><div><font color="#000000">Which based on this, I believe my question boils down to if I use a custom resultsource to select rows from the database, is </font><span style="color:rgb(0,0,0)">DBIx::Class::ResultSet</span><span style="color:rgb(0,0,0)"> still used and does it still call the inflate_result method.</span></div>
<div><font color="#000000"><br></font></div><div><font color="#000000">Based on the following from the Cookbook it looks like the answer to the questions is that it is using ResultSet and I presume it should be calling inflate_result, but it will need testing(unless someone already knows this).</font></div>
<div><br></div><div><a name="1464501ca52d473d_arbitrary_sql_through_a_custom_resultsource" href="http://perl.mines-albi.fr/perl5.8.5/site_perl/5.8.5/DBIx/Class/Manual/Cookbook.html#arbitrary_sql_through_a_custom_resultsource" style="font-family:Verdana,Arial,Helvetica,sans-serif;font-size:large;font-style:italic" target="_blank">Arbitrary SQL through a custom ResultSource</a><br>
</div><div><p style="text-align:justify;color:rgb(102,102,102);font-family:Verdana,Arial,Helvetica,sans-serif;font-size:16px">Sometimes you have to run arbitrary SQL because your query is too complex (e.g. it contains Unions, Sub-Selects, Stored Procedures, etc.) or has to be optimized for your database in a special way, but you still want to get the results as a <a href="http://perl.mines-albi.fr/perl5.8.5/DBIx/Class/ResultSet.html" style="color:rgb(0,102,255)" target="_blank">the DBIx::Class::ResultSet manpage</a>. The recommended way to accomplish this is by defining a separate ResultSource for your query. You can then inject complete SQL statements using a scalar reference (this is a feature of <a href="http://perl.mines-albi.fr/perl5.8.5/SQL/Abstract.html" style="color:rgb(0,102,255)" target="_blank">the SQL::Abstract manpage</a>).</p>
<p style="text-align:justify;color:rgb(102,102,102);font-family:Verdana,Arial,Helvetica,sans-serif;font-size:16px">Say you want to run a complex custom query on your user data, here's what you have to add to your User class:</p>
<pre style="margin-top:3px;margin-bottom:3px;border:1px solid rgb(170,170,170);padding:3px;color:rgb(170,0,0);font-family:Courier,monospace;font-size:14px;background-color:rgb(238,238,238)"> package My::Schema::User;
use base qw/DBIx::Class/;
# ->load_components, ->table, ->add_columns, etc.</pre><pre style="margin-top:3px;margin-bottom:3px;border:1px solid rgb(170,170,170);padding:3px;color:rgb(170,0,0);font-family:Courier,monospace;font-size:14px;background-color:rgb(238,238,238)">
# Make a new ResultSource based on the User class
my $source = __PACKAGE__->result_source_instance();
my $new_source = $source->new( $source );
$new_source->source_name( 'UserFriendsComplex' );
# Hand in your query as a scalar reference
# It will be added as a sub-select after FROM,
# so pay attention to the surrounding brackets!
$new_source->name( \<<SQL );
( SELECT u.* FROM user u
INNER JOIN user_friends f ON <a href="http://u.id" target="_blank">u.id</a> = f.user_id
WHERE f.friend_user_id = ?
UNION
SELECT u.* FROM user u
INNER JOIN user_friends f ON <a href="http://u.id" target="_blank">u.id</a> = f.friend_user_id
WHERE f.user_id = ? )
SQL</pre><pre style="margin-top:3px;margin-bottom:3px;border:1px solid rgb(170,170,170);padding:3px;color:rgb(170,0,0);font-family:Courier,monospace;font-size:14px;background-color:rgb(238,238,238)"> # Finally, register your new ResultSource with your Schema
My::Schema->register_source( 'UserFriendsComplex' => $new_source );</pre><p style="text-align:justify;color:rgb(102,102,102);font-family:Verdana,Arial,Helvetica,sans-serif;font-size:16px">Next, you can execute your complex query using bind parameters like this:</p>
<pre style="margin-top:3px;margin-bottom:3px;border:1px solid rgb(170,170,170);padding:3px;color:rgb(170,0,0);font-family:Courier,monospace;font-size:14px;background-color:rgb(238,238,238)"> my $friends = [ $schema->resultset( 'UserFriendsComplex' )->search( {},
{
bind => [ 12345, 12345 ]
}
) ];
... and you'll get back a perfect L<DBIx::Class::ResultSet>.</pre></div></div>
<br>_______________________________________________<br>
List: <a href="http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class" target="_blank">http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class</a><br>
IRC: <a href="http://irc.perl.org#dbix-class" target="_blank">irc.perl.org#dbix-class</a><br>
SVN: <a href="http://dev.catalyst.perl.org/repos/bast/DBIx-Class/" target="_blank">http://dev.catalyst.perl.org/repos/bast/DBIx-Class/</a><br>
Searchable Archive: <a href="http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk" target="_blank">http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk</a><br></blockquote></div><br></div>