Re: [CDBI] sth_to_objects on sth from direct SQL with joins
Matt S Trout <dbix-class-uY5y5/[email protected]>
| Newsgroups | gmane.comp.lang.perl.modules.class-dbi |
|---|---|
| Message-ID | <[email protected]> |
Oliver Jeeves wrote:
> Right, I don't know if the subject of this message really makes any
> sense, but at least it's an attempt.
>
> So anyway, my problem.
>
> I'm constructing a fairly complex search that could cover relationships
> between a number of tables. The way I'm doing this, is to manually build
> up the SQL query (ick, I know), execute it, and then use
> sth_to_objects() to convert the results to CDBI objects.
>
> According to this page on the wiki:
>
> http://wiki.class-dbi.com/wiki/Directly_execute_SQL
>
> This should work. Obviously this isn't official documentation.
>
> I've found that this doesn't work when you're joining tables that have
> similarly named columns.
>
> In particular, I'm using an object factory as I've described here:
>
> http://wiki.class-dbi.com/wiki/Implementing_an_object_factory
>
> and my overridden construct method is getting a type value from the
> joined table, not the table for the class that sth_to_objects was called on.
>
> Ideally, I'd just use something like DeepAbstractSearch or Sweet (yes
> Matt, or DBIx::Class),
*snigger*
More seriously, it's because it uses the column names off the $sth via (IIRC)
$sth->{NAME_lc}, so you can easily get round this by AS-ing the columns to
other names and then converting them back in your construct method - that's
what I did in Sweet, modulo some annoying fucking about to try and avoid going
over the 30-col column name limit that some databases are kind enough to enforce.
(and obYesMatt, this is why I didn't do it that way in DBIC. But it's still
easy enough to work around)