Du skal bruge Eager MongoDB-markøren til at gentage forespørgsler. Jeg vil redigere din kode for at gøre det nemmere for dig:
class UserRepository extends DocumentRepository {
public function getUserTemp($clauses = null)
{
/* Create query */
$qb = $this->createQueryBuilder()->eagerCursor(true);
/* Add clauses */
if($clauses)
{
// It's a way to initialize the query, you can use a select or something similar
$query = $qb->hydrate(true);
if(isset($clauses['id']))
$query -> field('id') -> equals($clauses['id']);
if(isset($clauses['firstName']))
$query -> field('firstName') -> equals($clauses['firstName']);
if(isset($clauses['lastName']))
$query -> field('lastName') -> equals($clauses['lastName']);
if(isset($clauses['email']))
$query -> field('email') -> equals($clauses['email']);
if(isset($clauses['password']))
$query -> field('password') -> equals($clauses['password']);
}
/* Return */
return $query
-> getQuery()
-> execute();
}
}
Nu kan du gentage resultatet med alle resultater som objekter indeni.
private function getUserAction()
{
$manager = $this -> get('doctrine_mongodb') -> getManager();
$repository = $manager -> getRepository('LogAnalyzerCoreBundle:User');
$users = $repository -> getUserTemp(array(
'firstName' => 'First'
));
//It's just an example, but here you can see how to get the query result data
foreach($users as $user){
$data[] = $user->getFirstName();
}
return $users;
}
Det er den bedste måde at gøre dette på, nogle gange er det nok at kalde toArray()-metoden, men det virker ikke altid.