Lazy population of has_a child records

[prev] [thread] [next] [Date index for 2005/06/08]

From: Christopher Laco
Subject: Lazy population of has_a child records
Date: 02:36 on 08 Jun 2005
I have a strange problem that may not really be a problem. First, a
little background chatter.

I've been working on getting Order/Checkout code into Handel. Up to
this point, all of the the Cart/CartItem code via CDBI has worked like
a champ. I'm in the process of applying the same basic parent/child
structure to Order/OrerItems. As part of the Order->new process, new
will optionally take a complete Cart object reference. In such cases,
I loop through the Carts' Items and copy their data into a new hash
for later use in Order->add_to_items($hash).

This is roughly:

    my $cart =3D Handle::Cart->new(...);
    my $cart->add_to_items({
        id =3D> 123456,
        sku =3D> 'sku1',
        price =3D> 1.23
    });

    my $order =3D Handel::Order->new($cart);

    [behind the scenees in new]
    my $items =3D $cart->items;
    while (my $item =3D $items->next) {
        my %copy =3D %{$item};

        # reset some values, do some calulations
        ....
        # add copy of cart item into order items
        $self->add_to_items(\%copy);
    };


No, much to my surprise, %copy is empty except for id eve though the
items class has:

__PACKAGE__->columns(Essential =3D> qw(id cart sku price));

Of course if I call something first:


    [behind the scenees in new]
    my $items =3D $cart->items;
    while (my $item =3D $items->next) {
        $item->sku; #this loads the data
        my %copy =3D %{$item};

        # reset some values, do some calulations
        ....
        # add copy of cart item into order items
        $self->add_to_items(\%copy);
    };


%copy now has all of the key/values. Does this mean that lazy
population and stopping it via Essential only works on the class you
are directly working with, and not child record classes returned via
has_a/has_many?

I guess it makes send not to populate child relational records until
they're needed, but in this instance, I feel dirty call a field just
to get it loaded. IS there a better way to override lazy population
behaviour in has_a relationships?

-=3DChris

Lazy population of has_a child records
Christopher Laco 02:36 on 08 Jun 2005

Re: Lazy population of has_a child records
Perrin Harkins 04:06 on 08 Jun 2005

Re: Lazy population of has_a child records
Peter Speltz 04:10 on 08 Jun 2005

Re: Lazy population of has_a child records
Tony Bowden 08:29 on 08 Jun 2005

Re: Lazy population of has_a child records
Christopher Laco 12:40 on 08 Jun 2005

Re: Lazy population of has_a child records
Christopher Laco 12:52 on 08 Jun 2005

Re: Lazy population of has_a child records
merlyn (Randal L. Schwartz) 16:24 on 08 Jun 2005

Generated at 16:36 on 28 Jul 2005 by mariachi v0.52