YMMD
YMMD

Reputation: 3780

PHP Lithium: Filtering an existent DocumentSet and get first Match

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?

Upvotes: 3

Views: 243

Answers (3)

YMMD
YMMD

Reputation: 3780

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.

Upvotes: 0

rmarscher
rmarscher

Reputation: 5642

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

Upvotes: 1

anon
anon

Reputation:

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?

Upvotes: 2

Related Questions