Getting references together with parent document

25 views
Skip to first unread message

Bar Ziony

unread,
Mar 7, 2013, 7:20:06 AM3/7/13
to mandang...@googlegroups.com
Hey,

I have this basic config:
 // assign the config classes
        $mondator->setConfigClasses(array(
            'Model\Category' => array(
                'collection' => 'categories',
                'fields' => array(
                    'name' => 'string',
                ),
                'referencesMany' => array(
                    'subcategories' => array('class' => 'Model\Category'),
                ),
                'relationsManyMany' => array(
                    'items' => array(
                        'class' => 'Model\Item',
                        'reference' => 'categories'
                    ),
                ),
            ),

            'Model\Item' => array(
                'collection' => 'items',
                'fields' => array(
                    'name' => 'string',
                ),
                'referencesMany' => array(
                    'categories' => array('class' => 'Model\Category'),
                ),
            ),
        ));


Each category can have subcategories (in the same collection, same model), and each category model (either sub category or category, it doesn't matter) can have one or more items.


Now I want to output in JSON all the effects and inside each effect, its categories.

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.

Pablo Díez

unread,
Mar 8, 2013, 10:44:22 AM3/8/13
to mandang...@googlegroups.com
You should implement a way to output to JSON in the documents you want. You can also create a behavior for this.
 

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?

What is manual? What do you want to do automatically? )
 

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.
 
 

Bar Ziony

unread,
Mar 9, 2013, 7:10:03 AM3/9/13
to mandang...@googlegroups.com
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?

Thanks!
Bar.

Pablo Díez

unread,
Mar 9, 2013, 7:31:36 AM3/9/13
to mandang...@googlegroups.com
On Sat, Mar 9, 2013 at 1:10 PM, Bar Ziony <b...@photomania.net> wrote:
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();
    }

You can implement this in the category instead of outside. Maybe a toArrayWithItems or something like that.
 

    $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?

Just use functions like map:

$categoriesToArrayWithItems = array_map(function ($category) {
    return $category->toArrayWithItems();
}, $categories);


$categoriesToArrayWithItems = map(method('toArrayWithItems'), $categories);

Bar Ziony

unread,
Mar 9, 2013, 7:40:34 AM3/9/13
to mandang...@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() ?

Thanks,
Bar.



--
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 Díez

unread,
Mar 9, 2013, 7:47:24 AM3/9/13
to mandang...@googlegroups.com
On Sat, Mar 9, 2013 at 1:40 PM, Bar Ziony <b...@photomania.net> wrote:
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?

It depends on how many times you're going to reuse that.
 

Also - if I change toArray() itself - would that have an impact on Mandango?

If you only change toArray of one class no, but it's probably better to leave it with its original behavior and implement another method, which can be based on toArray.
 


About array_map - yeah, it's possible, but still less cool than:
return json_encode($categories = $mandango
    ->getRepository('Model\Category')
    ->createQuery()
    ->references("items")
    ->all());

Less cool for you :P
 

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() ?

What you save is one method call, references, so it is up to you! ;)

Bar Ziony

unread,
Mar 9, 2013, 8:14:06 AM3/9/13
to mandang...@googlegroups.com

How come I'm saving only one call?

Thanks,
Bar.

Pablo Díez

unread,
Mar 9, 2013, 8:40:36 AM3/9/13
to mandang...@googlegroups.com
On Sat, Mar 9, 2013 at 2:14 PM, Bar Ziony <b...@photomania.net> wrote:

How come I'm saving only one call?


$query->references(array('items'))->all();

vs

$query->allWithReferences();

Bar Ziony

unread,
Mar 9, 2013, 9:04:32 AM3/9/13
to mandang...@googlegroups.com
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?

Am I wrong?

Thanks again,
Bar.

Pablo Díez

unread,
Mar 10, 2013, 8:35:50 AM3/10/13
to mandang...@googlegroups.com
On Sat, Mar 9, 2013 at 3:04 PM, Bar Ziony <b...@photomania.net> wrote:
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?

And do you want to implement that logic in the query? ;)

Bar Ziony

unread,
Mar 10, 2013, 10:26:40 AM3/10/13
to mandang...@googlegroups.com
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?

Thanks,
Bar.

Pablo Díez

unread,
Mar 10, 2013, 2:54:11 PM3/10/13
to mandang...@googlegroups.com
On Sun, Mar 10, 2013 at 3:26 PM, Bar Ziony <b...@photomania.net> wrote:
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()).

->references() in queries is only for querying more efficiently. What you want to do can be done with or without calling ->references().
 

What's your take?

Queries should be for querying, what you want to do is not querying, but processing data.
Reply all
Reply to author
Forward
0 new messages