coen-hyde / Shanty-Mongo

Shanty Mongo is a mongodb library for the Zend Framework. Its intention is to make working with mongodb documents as natural and as simple as possible. In particular allowing embedded documents to also have custom document classes.
Other
200 stars 52 forks source link

"Pure" arrays not accessible in a Document #55

Open Alex-PK opened 12 years ago

Alex-PK commented 12 years ago

I am not sure if this is a bug or not.

I tried storing a "pure" array in a Document, like this:

$doc = Shanty_Mongo_Document:.create(); $doc->val = array('a','b','c'); $doc->save();

When I try to reload it, I get an error:

$doc = Shanty_Mongo_Document::find(...); print implode(',', $doc->val);

Warning: implode(): Invalid arguments passed...

print_r($doc->val);

Shanty_Mongo_Document Object ( [_docRequirements:protected] => Array ( [_id] => Array ( [Validator:MongoId] => ) [_type] => Array ( [Array] => ) ) [_data:protected] => Array ( ) [_cleanData:protected] => Array ( 0 => 'a', 1 => 'b', 2 => 'c') ...

It seems that it transforms the array in a sub-document, but only during __get, as if I print_r($doc) everything seems fine. Browsing the source (0.3.0) I found this in Shanty_Mongo_Document (row 562):

if ($this->hasRequirement($property, 'Array')) { return $this->_data[$property] = $data; }

that leads me to thinking that I can force a requirement Array in the class, so I did it:

protected static $_requirements = array('val'=>'Array');

but then I get an exception:

Shanty_Mongo_Exception: No requirement exists for 'Array'

I solved it modifying Shanty_Mongo, adding on line 41:

static::storeRequirement('Array', new Shanty_Mongo_Validate_StubTrue());

This way everything works well (reading, writing, etc), or so it seems. :) Is my patch correct or did you have something else in mind?

gwagner commented 12 years ago

I have also found the control between making something a document / document_set / array slightly confusing and poorly documented. What is the method to the madness? I would be happy to rework the code or the documentation.

rremigius commented 12 years ago

I also noticed that arrays, once stored in the DB, are retrieved as Shanty_Mongo_Document. I check whether the value is an array or a Shanty_Mongo_Document and when it's the latter, I use the export() method to get the original array.

I'm not sure if it is necessary to retrieve arrays as Documents from the DB, but it would indeed be more convenient if you got what you put in: an array.

By the way, if you change the requirement 'val' => 'Array' into 'val' => 'Validator:Array', it won't give you an error. The array returned as Document issue will still be there, however. I'm going to try your patch.

ghost commented 12 years ago

+1 for making the distinction between Document, DocumentSet and Array more clear.

tholder commented 12 years ago

+1

I can't help but feel we need to give the whole requirements/filter code a serious re-work.

ghost commented 12 years ago

For what it's worth - I took a different route and wrote my own component for my ZF project. The thing is - the PHP mongo driver is really nice and easy to use and doesn't need that much 3rd party abstraction, really. I just took the general idea (the bits that I understood) of Shanty-Mongo (Collections being static and only instantiating documents) and no DocumentSets and must say things have been clearer and much more predictable now.

just my 2c...