Using relationships to search in DBIx::Class
I am starting learning DBIx::Class and I have 开发者_Python百科a doubt in searching in a related table:
Consider the following code:
my $books = $author->search_related('books', { name => 'Titanic' });
my $books = $author->books->search({name => 'Titanic'});
What I want is to only searches for books named 'Titanic' by the author in $author
.
search_related
is a Resultset method. You'd use that if you had a resultset of Authors and you wanted to get a resultset of all of their books named 'Titanic'.
my $books = $schema->resultset('Author')->search({ last_name => 'Smith' })
->search_related('books', { name => 'Titanic' });
If $author
is a row object, representing one row, then your second line is how you'd search his books.
my $books = $author->books->search({ name => 'Titanic' });
The distinction between rows and resultsets is one of the core concepts of DBIx::Class. You might want to review the DBIC Manual Intro. #dbix-class on irc.perl.org is usually pretty active so you can find help there as well.
精彩评论