使用关系在 DBIx::Class 中搜索
我正在开始学习 DBIx::Class,并且我对在相关表中搜索有疑问:
考虑以下代码:
my $books = $author->search_related('books', { name => 'Titanic' });
my $books = $author->books->search({name => 'Titanic'});
我想要的是仅在 $author
.
这两个搜索返回相同的结果集?
如果是,最好的方法是什么?为什么?
如果不是,有什么区别?
I am starting learning DBIx::Class and I have 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
search_lated
是一种结果集方法。如果您有一个作者结果集,并且您想要获得他们所有名为“泰坦尼克号”的书籍的结果集,则可以使用它。如果
$author
是一个行对象,代表一行,那么第二行就是您搜索他的书籍的方式。行和结果集之间的区别是 DBIx::Class 的核心概念之一。您可能需要查看DBIC 手册简介。 irc.perl.org 上的 #dbix-class 通常非常活跃,因此您也可以在那里找到帮助。
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'.If
$author
is a row object, representing one row, then your second line is how you'd search his books.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.