Sorting an embedded DocumentArray: No model bound or unhandled method call `sort`. #405

Closed
ymmd opened this Issue Mar 29, 2012 · 3 comments

Comments

Projects
None yet
3 participants

ymmd commented Mar 29, 2012

Hey! :-)

I would like to sort an array of objects which is embedded in a document. The API states that there is a method sort for Collections and due to the fact that DocumentArray extends this, I guess it also should be available there. I wrote a simple test, because it seems as if the method is not even there:

public function testSort(){
    $thread = Thread::create(array(
                'postings' => array(
                    array('text' => 'hey',    'upvotes' => 15),
                    array('text' => 'my',     'upvotes' => 23),
                    array('text' => 'dear',   'upvotes' => 16),
                    array('text' => 'friend', 'upvotes' => 42),
                )
            ));
    $thread->save();
    $thread->postings->sort('upvotes');
}

The errors keep telling me that there is no method sort():

Exception thrown in lithium\data\Entity::__call() on line 191

No model bound or unhandled method call sort.

Trace

lithium\data\entity\Document::sort(), line ??
lithium\util\Collection::invoke(), line 203
lithium\util\Collection::__call(), line 217
lithium\data\collection\DocumentArray::sort(), line ??
app\tests\cases\models\SortDocumentArrayTest::testSort(), line 23
lithium\test{closure} @ ?, line 660
lithium\core\Object::_filter(), line 240
lithium\test\Unit::_runTestMethod(), line 664
lithium\test\Unit::run(), line 186
lithium\util\Collection::invoke(), line 203
lithium\util\Collection::__call(), line 217
lithium\util\Collection::run(), line ??
lithium\test\Report::run(), line 142
lithium\test\Dispatcher::run()::{closure} @ ?, line 66
lithium\core\StaticObject::_filter(), line 119
lithium\test\Dispatcher::run(), line 70
lithium\test\Controller::__invoke()::{closure} @ ?, line 49
lithium\core\Object::_filter(), line 240
lithium\test\Controller::__invoke(), line 59
lithium\action{closure} @ ?, line 223
lithium\core\StaticObject::_filter(), line 119
lithium\action\Dispatcher::_call(), line 226
lithium\core\StaticObject::invokeMethod(), line 75
lithium\action\Dispatcher::run()::{closure} @ ?, line 123
lithium\util\collection\Filters::next(), line 202
D:\xampp\htdocs\AWF\app\config\bootstrap\action.php::{closure} @ 42, line 52
lithium\util\collection\Filters::next(), line 202
D:\xampp\htdocs\AWF\app\config\bootstrap\cache.php::{closure} @ 47, line 54
lithium\util\collection\Filters::run(), line 183
lithium\core\StaticObject::_filter(), line 126
lithium\action\Dispatcher::run(), line 124
[main], line 41

What's wrong with this method? Can't I use it that way? Thank you very much in advance for any kind of help!

Member

mackstar commented Mar 29, 2012

This is not actually supported by MongoDb or other document databases in general. It may be possible with some kind of map reduce but is not regular supported functionality of the database.

Contributor

mehlah commented Mar 29, 2012

@mackstar As I understand from that code, @ymmd do a sort() on a DocumentArray object already fetched from a database. This is not done by Mongo.

@ymmd Your test is all green for me! (I only removed the call to save()).
$thread->postings should be a DocumentArray, double check that.

What version of Lithium are you running ?

ymmd commented Mar 30, 2012

@mackstar: Sorry, if my code mislead you into thinking I did a database operation. @mehlah: Yes, right, $thread is a lithium\data\entity\Document and $thread->postings is a lithium\data\collection\DocumentArray. So they should be able to sort themselves, shouldn't they? I am using Litium 0.10 which I thought was the latest version. I just had a look into the source code of the classes mentioned above and I really did not find a sort() method. Is my version outdated?

Appendix: I just copied the method from github directly into the code. It works. I downloaded Lithium on March 11 2012. Although the history in github states that the code hasn't changed since much longer, it was missing in my code. So either there's magic or I really fucked up something. Thanks for all your help.

ymmd closed this Mar 30, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment