我正在学习Perl MongoDBX :: Class并编写博客应用程序。
Bellow是Post and Comment模型。如果调用此方法:
my $comments = $a_post->comments;
如果此帖没有评论,它会暂停该应用程序。问题是如何检查这篇文章是否有评论?
感谢。
package Model::Schema::Post;
use MongoDBx::Class::Moose;
use namespace::autoclean;
with 'MongoDBx::Class::Document';
has 'title' => (is => 'rw', isa => 'Str', required => 1,);
belongs_to 'author' => (is => 'ro', isa => 'Author', required => 1);
has 'post_date' => (is => 'ro', isa => 'DateTime', traits => ['Parsed'], required => 1);
has 'text' => (is => 'rw', isa => 'Str', required => 1);
joins_many 'comments' => (is => 'ro', isa => 'Comment', coll => 'comments', ref => 'post');
holds_many 'tags' => (is => 'rw', isa => 'Tag', predicate => 'has_tag');
__PACKAGE__->meta->make_immutable;
package Model::Schema::Comment;
use MongoDBx::Class::Moose;
use namespace::autoclean;
with 'MongoDBx::Class::Document';
belongs_to 'post' => (is => 'ro', isa => 'Post', required => 1);
has 'author' => (is => 'ro', isa => 'Author', required => 1);
has 'comment_date' => (is => 'ro', isa => 'DateTime', traits => ['Parsed'], required => 1);
has 'text' => (is => 'rw', isa => 'Str', required => 1);
has 'rateing' => (is => 'rw', isa => 'Int');
__PACKAGE__->meta->make_immutable;
答案 0 :(得分:0)
我不确定你的应用程序停止的原因。查看您的架构,运行
my $comments = $a_post->comments;
应该返回一个MongoDBx :: Class :: Cursor对象(实际上只是一个MongoDB :: Cursor对象)。在此光标上,您可以运行:
my $num_comments = $comments->count;
if ($num_comments > 0) {
my @comments = $comments->all;
...
}
如果这对您没有帮助,并且您的应用程序仍然挂起,如果您向我发送代码示例,我会帮助我,以便我可以尝试找出正在发生的事情。
顺便说一下,我是MongoDBx :: Class的作者。
P.S。如果你觉得你偶然发现MongoDBx :: Class中的一些错误,请随意打开一个错误报告,如CPAN上的MongoDBx :: Class页面所述。