[Dbix-class] How to create a row object that contains related objects in DBIx::Class?

Ian Docherty dbix-class at iandocherty.com
Thu Aug 16 11:21:22 GMT 2012


On 16 August 2012 08:38, Eugene Yarmash <e.yarmash at gmail.com> wrote:
> Hello and thank you to everyone who takes part in the development of
> DBIx::Class. I have a question (also posted at SO
> http://stackoverflow.com/q/11970939/244297).
>
> 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 related
> object can be accessed without the additional query?
>
One technique I have seen used is as follows.

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

Be warned however, in some circumstances this can lead to circular
dependencies causing memory leaks.

- ian



More information about the DBIx-Class mailing list