coen-hyde/Shanty-Mongo — Issue #55: "Pure" arrays not accessible in a Document
Author: Alex-PK
Created: 2011-11-29 14:36:56+00:00
State: open

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?