Fix 577 opens up issue with subdocuments and data() calls #732

Closed
fellars opened this Issue Dec 12, 2012 · 7 comments

Projects

None yet

3 participants

@fellars

Reference: Fix #577

I don't think this was what I was hoping for with regard to subdocuments and not indexing. By changing the data() call to have 'indexed' be null and checking if it has a parent, this breaks simple sub arrays

{
"_id": {"$oid": MongoID("abc")},
"subItems": [{
    "a":"value"
}]
}
foreach($item->subItems as $subItem){
    $data = $subItem->data();
    //data is now just an array of the values (array("value"))
   //instead of being indexed array("a"=>"value")
  //that is a pretty big change!!!
}

My previous notes regarding this issue was the magic of subdocuments using the _id field as the key for non-indexed arrays within subdocuments, not that all subdocument data should not be indexed, which appears to have been the change here.

@jails
Union of RAD member

Ok, can you provide a full example with used schema ?

@fellars

ok, looks like i figured out what is going on. I have a workaround for my code and it may not be a bug but something that may come up.

If creating an array of array's where the second array is empty, it will get cast into a DocumentSet and not a Document, which is what was causing my problems.

Example:

$entity = Model::create();

$entity->items = array(array());

This converts into a DocumentSet of DocumentSets instead of a DocumentSet of Documents (which is what I was thinking I had)

But, if that 2nd layer has any associated data in it, it will create a DocumentSet of Documents instead.
Example:


$entity->items = array(array('a'=>true));

I'm good going forward now that I know what is going on and can manage the code on my end to make sure a Document gets created, but wondering if the default in this scenario should be to create Documents if array is empty and not DocumentSets???

@jails
Union of RAD member

It's hard to say if it's a correct behavior or a bug. It essentially depends on the schema you are using for your Model.

@jails
Union of RAD member

Any news on this issue ? Should I tag it to "bug" ?

@fellars
@jails
Union of RAD member

The type Document or DocumentSet essentially depends of the defined schema.

If you set:

'yourfield' => array('type' => 'object', 'array' => true)

It should be casted to DocumentSet and inner elment to Document whether the content are empty or not.

If you set:

'yourfield' => array('type' => 'object')

it should cast to Document whether the content are empty or not.

@fellars
@jails jails added a commit that referenced this issue Dec 17, 2012
@jails jails Close #732. ec6f75a
@gwoo gwoo closed this Dec 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment