开发者

Using relationships to search in DBIx::Class

开发者 https://www.devze.com 2023-03-28 20:28 出处:网络
I am starting learning DBIx::Class and I have 开发者_Python百科a doubt in searching in a related table:

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.

This two searches return the same resultset?

If yes, what is the best way and why?

If no, what is the difference?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号