Question

I am retrieving a DocumentSet in Lithium from MongoDB, but I don't want to process the documents all at once. Instead I would like to have a filter, which I just could tell something like this:

$manyDocuments->giveMeTheOneWhere(array('foo' => 'bar'));

I already tried to do it this way, but it didn't work:

$manyDocuments->find(function($singleDocument){
    return ($singleDocument->foo == 'bar');
});

Even if I manually return true inside the closure, it always returns an empty DocumentSet.

Just to add clarity: I am not looking for a database-operation, instead I want to get one out of an already existent DocumentSet. Is there a fancy way to achieve this or do I need to iterate through the set using a custom function?

Was it helpful?

Solution 3

Instead of using find() I just used first() with a closure. This works as expected. Sadly that was the only thing I didn't try before. Excuse me for answering my own question.

Anyway I'd still be interested in a way to get another Document Set.

OTHER TIPS

That looks right to me. Is that the exact code you are using?
For example, is the 'bar' value you are using something you are passing in?

I'm on the latest of the master branch of Lithium and wrote this unit test which works for me. I'm not really sure why you're getting an empty DocumentSet.

$docs = new DocumentSet(array('data' => array(
    new Document(array('data' => array(
        'id' => 1,
        'foo' => 'bar'
    ))),
    new Document(array('data' => array(
        'id' => 2,
        'foo' => 'baz'
    ))),
    new Document(array('data' => array(
        'id' => 3,
        'foo' => 'bar'
    ))),
    new Document(array('data' => array(
        'id' => 4,
        'blah' => 'ack'
    )))
)));
$filtered = $docs->find(function($doc) {
    return $doc->foo === 'bar';
});
$expected = array(
    '0' => array('id' => 1, 'foo' => 'bar'),
    '2' => array('id' => 3, 'foo' => 'bar')
);
$this->assertIdentical($expected, $filtered->data());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top