2013-03-27 39 views
2

我用猫鼬设置如下(简化和压缩)的数据模型:如何调用所有()上的猫鼬::加入对象

package Model::Tag; 
use Moose; 
use Mongoose::Class; with 'Mongoose::Document'; 
has 'value'   => (is => 'rw', isa => 'Str', required => 1); 
no Moose; 
__PACKAGE__->meta->make_immutable; 

package Model::Document; 
use Moose; 
use Mongoose::Class; with 'Mongoose::Document'; 
has 'title'   => (is => 'rw', isa => 'Str', required => 1); 
has_many 'tags'  => (is => 'rw', isa => 'Model::Tag'); 
no Moose; 
__PACKAGE__->meta->make_immutable; 

我测试的重要部分是:

package main; 
use strict; 
use warnings; 
use Data::Dumper; 

my $expected; my $got; 

my $doc = Model::Document->new(title => 'My new document with many tags'); 
my $tag1 = Model::Tag->new(value => 'foo'); 
my $tag2 = Model::Tag->new(value => 'bar'); 

my $x1 = $doc->tags->add($tag1); 
my $x2 = $doc->tags->add($tag2); 

# print "x1 = $x1 x2 = $x2 \n"; 

my $document_tags = $doc->tags; 
print Dumper $document_tags; 

can_ok($document_tags, 'all'); 

my $tag_array_ref = $document_tags->all(); 

现在的问题:

$ document_tags的倾倒输出是猫鼬::加入对象:

$VAR1 = bless({ 
      'delete_buffer' => {}, 
      'with_class' => 'Model::Tag', 
      'buffer' => { 
          '58102804' => bless({ 
                'value' => 'foo' 
               }, 'Model::Tag'), 
          '58069732' => bless({ 
                'value' => 'bar' 
               }, 'Model::Tag') 
         } 
      }, 'Mongoose::Join'); 

和文档有关Mongoose::Join列表的方法:

add, remove, find, find_one, first, all, hash_on, hash_array, query, collection, with_collection_name 

但调用

$document_tags->all(); 

导致错误

Can't use an undefined value as a HASH reference at C:/Perl/site/lib/Mongoose.pm line 132. 

问题是什么?

在此先感谢您的帮助和意见。

回答

0

错误消息来自Mongoose,而不是您的测试程序直接。最近版本的Mongoose(0.23)中的Mongoose.pm 132行是在建立到MongoDB连接的方法中。

继此之后,我发现您的测试代码不包含对Mongoose->db()的调用。这是为了指定要连接的数据库所必需的。在添加并测试您的测试程序后,我还注意到您的文档在尝试从中检索属性(即标记)之前未保存到MongoDB。

这是您的测试代码,包含我为解决问题所做的更改。密钥位以行# Connect to database开头。

package Model::Tag; 
use Moose; 
use Mongoose::Class; with 'Mongoose::Document'; 
has 'value'   => (is => 'rw', isa => 'Str', required => 1); 
no Moose; 
__PACKAGE__->meta->make_immutable; 

package Model::Document; 
use Moose; 
use Mongoose::Class; with 'Mongoose::Document'; 
has 'title'   => (is => 'rw', isa => 'Str', required => 1); 
has_many 'tags'  => (is => 'rw', isa => 'Model::Tag'); 
no Moose; 
__PACKAGE__->meta->make_immutable; 

package main; 
use strict; 
use warnings; 
use Data::Dumper; 

my $doc = Model::Document->new(title => 'My new document with many tags'); 
my $tag1 = Model::Tag->new(value => 'foo'); 
my $tag2 = Model::Tag->new(value => 'bar'); 

my $x1 = $doc->tags->add($tag1); 
my $x2 = $doc->tags->add($tag2); 

my $document_tags = $doc->tags; 
print Dumper $document_tags; 

# Connect to database 
Mongoose->db('foo'); 
# and save our document first 
$doc->save(); 

# now we can retrieve the array (note, not array ref) of tags 
my @tag_array = $document_tags->all(); 
print Dumper(\@tag_array); 
+0

我知道如何将值存储在Mongodb中。但我没想到我必须存储这些值才能够再次读取它们。这很奇怪 – smartmeta 2013-04-03 08:07:30

+0

它看起来像从'Mongoose :: Join'文档的字面解读 - 'all'是'find'的包装器,它依次查询MongoDB。它不检查由'add'添加的未保存文档的缓冲区;对于猫鼬维护者来说是否应该是一个问题。 – 2013-04-04 04:43:18

+0

是的,但维护人员建议在Stackoverflow上进行询问。 :-( – smartmeta 2013-04-05 07:11:39