2

When you create a Row object in DBIx::Class you can pass related objects as values, e.g.

my $author = $authors_rs->find(1);
my $book = $books_rs->create({ author => $author, title => 'title' });

However, if you later use the author accessor, the object is retrieved again from the database. Is it possible to create an object so that the associated object can be accessed without the additional query?

2
  • 1
    That's a good question. You might want to ask it on the DBIx::Class mailing list, where the developers hang out. It seems to me that caching the author object in the book's scope after the creation should be possible. But it might be that DBIC always biases towards retrieving the related record to insure freshness.
    – Len Jaffe
    Commented Aug 15, 2012 at 20:04
  • Posted to the DBIx::Class mailing list: lists.scsys.co.uk/pipermail/dbix-class/2012-August/010741.html Commented Aug 16, 2012 at 8:33

2 Answers 2

0

How about just copying what you want from $author into some plain old Perl variables?

If you want to copy a whole structure, the clone module may help (I don't have experience with this module; I just found it online).

0

I'm not sure that I'm understanding you correctly, but if I am, perhaps you want to look into the prefetch functionality to automatically be ready for calling those other related row objects.

For example, in Galileo, when listing all pages (articles) I use this mechanism to get the related author objects for each page object (see here).


Ok so if the point is to store one object in another, you might want to inject some extra data into the object.

UNTESTED:

## some initial checks (run these only once)

# check that method name is available
die "Cannot use method 'extra_data'" if $book->can('extra_data');

# check that the reftype is a hash
require Scalar::Util;
die "Incorrect underlying type" unless Scalar::Util::reftype($book) eq 'HASH';

# check that the key is available
die "Key unavailable" if exists $book->{'my_extra_data'};

{
  no strict 'refs';
  # create a simple accessor for a hash stored in an object
  *{ ref($book) . '::extra_data' } = sub {
    my $self = shift;

    #return all extra data if called without args
    return $self->{my_extra_data} unless @_; 

    my $key = shift;
    if (@_) {
      $self->{my_extra_data}{$key} = shift;
    }

    return $self->{my_extra_data}{$key};
  };
}

$book->extra_data( author => $author );

#then later

my $stored_author = $book->extra_data('author');
2
  • prefetch is used for JOINs (i.e. SELECTs) while I have a different use case. I have a related object already and want to have it stored in the newly created object (which has the foreign key) so that it doesn't need to be retrieved again (in different file/scope of code) Commented Aug 15, 2012 at 17:13
  • ok, so you have some code which return a row object into another scope (otherwise you would need another SELECT). Can't you just return both objects instead of one? Perhaps you want to return a collection "object" which contains both? What i'm saying is that by whatever channel you are moving your row object, you should be able to move both, I would think. Commented Aug 15, 2012 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.