php - Symfony QueryBuilder returning MongoDB cursor instead of objects array -
i trying build custom querying function returning mongodb documents corresponding filters. have created function specific repository document user
:
namespace loganalyzer\corebundle\repository; use doctrine\odm\mongodb\documentrepository; class userrepository extends documentrepository { public function getusertemp($clauses = null) { /* create query */ $query = $this -> createquerybuilder(); /* add clauses */ if($clauses) { 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(); } }
here definition of user
document:
namespace loganalyzer\corebundle\document; use jsonserializable; use doctrine\odm\mongodb\mapping\annotations mongodb; use symfony\component\validator\constraints assert; /** * @mongodb\document(repositoryclass="loganalyzer\corebundle\repository\userrepository") */ class user implements jsonserializable { /** * @mongodb\id */ protected $id; /** * @mongodb\string */ protected $firstname; /** * @mongodb\string */ protected $lastname; /** * @mongodb\string */ protected $email; /** * @mongodb\string */ protected $password; ... }
in controller have private function using querying function:
private function getuseraction() { $manager = $this -> get('doctrine_mongodb') -> getmanager(); $repository = $manager -> getrepository('loganalyzercorebundle:user'); $users = $repository -> getusertemp(array( 'firstname' => 'first' )); return $users; }
when run function, not array of objects expected doctrine\odm\mongodb\cursor
instead.
where error ?
edit:
should use -> toarray(false)
?
thanks.
you need eager mongodb cursor iterate queries. i'm gonna edit code make easier you:
class userrepository extends documentrepository { public function getusertemp($clauses = null) { /* create query */ $qb = $this->createquerybuilder()->eagercursor(true); /* add clauses */ if($clauses) { // it's way initialize query, can use select or 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(); } }
now can iterate result results objects inside.
private function getuseraction() { $manager = $this -> get('doctrine_mongodb') -> getmanager(); $repository = $manager -> getrepository('loganalyzercorebundle:user'); $users = $repository -> getusertemp(array( 'firstname' => 'first' )); //it's example, here can see how query result data foreach($users $user){ $data[] = $user->getfirstname(); } return $users; }
it's best way this, calling toarray() method enough, not work.
Comments
Post a Comment