Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

"Pure" arrays not accessible in a Document #55

Open
Alex-PK opened this issue Nov 29, 2011 · 5 comments
Open

"Pure" arrays not accessible in a Document #55

Alex-PK opened this issue Nov 29, 2011 · 5 comments

Comments

@Alex-PK
Copy link

Alex-PK commented Nov 29, 2011

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
Copy link

gwagner commented Nov 29, 2011

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
Copy link

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
Copy link

ghost commented Feb 27, 2012

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

@tholder
Copy link
Collaborator

tholder commented Mar 26, 2012

+1

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

@ghost
Copy link

ghost commented Mar 28, 2012

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...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants