Doing this:$items = $mandango->getRepository('Model\Item')->createQuery()->references(array('categories'));How can I now get $items (which is a Query object) data into JSON? I -have- to iterate over it? When I do for each item ->getCategories() - I get a ReferenceGroup object, how can I get all the categories documents from that ? Also only by iteration?
This is pretty manual, and very similar to just using MongoDB natively with PHP, unless I'm missing something?
--Thanks!Bar.
You received this message because you are subscribed to the Google Groups "Mandango Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mandango-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Pablo,I know of the all() method, what I'm saying is this method returns an array of Model elements (let's say my model is Model\Category, so I'm getting an array of Model\Category objects).to get the data (i.e. toArray(), in order to output it to the client) of these objects AND their reference data, I need to do something like this:$categories = $mandango->getRepository('Model\Category')->createQuery()->references("items")->all();
$output = array();
foreach ($categories as $category) {$items = array();foreach ($categories->getItems()->createQuery() as $item) {$items[] = $item->toArray();}
$category_array = $category->toArray();$category_array['items'] = $items;$output[] = $category_array;}return $output;I just think that what should be returned from all() is some kind of Collection/Repository object, and not an array, and that collection/repository object should have a toArray() method that works for all its elements (model objects) and resolves their references and does the same.
Unless you have a different suggestion?
--
You received this message because you are subscribed to a topic in the Google Groups "Mandango Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mandango-users/FeQJJ9YD6q4/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mandango-user...@googlegroups.com.
Pablo,
Thanks for the quick answer.What do you think about implementing toArrayWithItems() in an extension so I can use it in a project without writing it in each document class?
Also - if I change toArray() itself - would that have an impact on Mandango?
About array_map - yeah, it's possible, but still less cool than:return json_encode($categories = $mandango->getRepository('Model\Category')->createQuery()->references("items")->all());
in the controller... :) So I thought about extending the functionality of this with an extension as well. Do you think it's just best to add a method to Query (with an extension) like allWithReferences() ?
How come I'm saving only one call?
Thanks,
Bar.
How come I'm saving only one call?
Pablo,
Yep, but the 1st one returns an array of documents, where if you want to get the referenced documents (items model in the example) - you need to call getItem() for each element in the array?
Well,I don't know of any other place that "knows" what did I reference to (i.e. ->createQuery()->references(array(...))). I would prefer to do it in the model itself, like Eloquent is doing with RDBMS for example (toArray() there merges between $this->getAttributes() and $this->getRelationsAttributes()).
What's your take?