使用关系在 DBIx::Class 中搜索

发布于 2024-11-30 06:58:07 字数 323 浏览 1 评论 0原文

我正在开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2024-12-07 06:58:07

search_lated 是一种结果集方法。如果您有一个作者结果集,并且您想要获得他们所有名为“泰坦尼克号”的书籍的结果集,则可以使用它。

my $books = $schema->resultset('Author')->search({ last_name => 'Smith' })
    ->search_related('books', { name => 'Titanic' });

如果 $author 是一个行对象,代表一行,那么第二行就是您搜索他的书籍的方式。

my $books = $author->books->search({ name => 'Titanic' });

行和结果集之间的区别是 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'.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文