-1

sBased on Doctrine documentation $qb->getQuery()->execute(); will return a cursor for you to iterator over the results but $qb->find($criteria); returns the actual found Documents.

I am using symfony2 MongoDBbundle and I would like to avoid iterating over the result set in Repository classes.

// Returns Product Document 
$entity = $this->get('doctrine_mongodb')
               ->getRepository("MyBundle:Product")
               ->findOneBy(array('title' => 'somthing'));


// Returns Cursor
$entity = $this->get('doctrine_mongodb')
               ->getRepository("MyBundle:Product")
               ->customFunctionWithcreateQueryBuilder(array('title' => 'somthing'));

How can I make cutomFunctionWithcreateQueryBuilder() returns the same class/result as findOneBy?

Also How can I make execute() returns all embedded documents?

EDIT

Content Of cutomFunctionWithcreateQueryBuilder:

class ProductRepository extends DocumentRepository {

public function customFunctionWithcreateQueryBuilder($param, $hydrate = true) {

        $query = $this->createQueryBuilder()
                ->select()
                ->hydrate($hydrate);

        if (isset($param['unique_id'])) {
            $query->field('id')->equals($param['unique_id']);
        }

        return $query->getQuery()->execute();
    }

}
pmoubed
  • 3,842
  • 10
  • 37
  • 60

1 Answers1

1

You can use getSingleResult() if you want only one result and set eagerCursor to true to get all data at once. In example:

    [...] 

    if (isset($param['unique_id'])) {
        $query->field('id')->equals($param['unique_id']);
    }

    return $query->eagerCursor(true)->getQuery()->getSingleResult();


    [...]  
Tom Tom
  • 3,680
  • 5
  • 35
  • 40