[Dbix-class] Hiding storage details
Andreas Mock
andreas.mock at drumedar.de
Tue Mar 17 22:33:15 GMT 2009
> -----Ursprüngliche Nachricht-----
> Von: "Jess Robinson" <castaway at desert-island.me.uk>
> Gesendet: 17.03.09 15:05:34
> An: "DBIx::Class user and developer list" <dbix-class at lists.scsys.co.uk>
> Betreff: Re: [Dbix-class] Hiding storage details
Hi Jess,
> Some diagrams would go a long way to explaining whats going on there.. let
> me take some guesses:
>
> Table O:
> ID | colour | attr2
> ----------------------
> 1 | red | 2
> 2 | orange | 4
> 3 | yellow | 6
>
> Table Attribute:
> O_id | Class | Value
> ----------------------
> 1 | length | 3cm
> 1 | height | 4cm
> 3 | length | 2cm
>
> Anywhere close?
Best guess... :-)
> And you'd like:
>
> my $row1 = $schema->resultset('O')->find(1);
>
> $row1->colour - returns red
> $row1->length - returns '3cm'
>
> which in normal dbic would be:
>
> $row1->attributes->find({ Class => 'length'})->Value
Yes, this would expose the storage details to the "user" of the class.
But this sounds like there is also "abnormal" DBIC. ;-)
>
> > Is there a way in DBIC to do this?
>
> There's always a way.. You can add more logic to your O class, so that its
> transparent to the rest of the system.
> I think you'll need autoload in your O clas, aka:
>
> sub AUTOLOAD {
> my $self = shift;
> my $attr_name = $AUTOLOAD;
> $attr_name =~ s/.*://;
>
> my $attr = $self->attributes->find({ class => $attr_name });
>
> die "No such attribute $attr_name" if(!$attr);
>
> return $attr->value;
> }
>
> Not convinced this is the best solution.. maybe someone will come up with
> a better one now I've suggested it!
Ok, that sounds feasible.
But other ideas are also welcome...
Anyway, Jess, thank you for taking the time to outline a solution.
Best regards
Andreas Mock
More information about the DBIx-Class
mailing list