symfony - MongoDB With Symfony2 -
i have make auto suggest on city level have list cities of mapped respective countries usa => ( "new york", "dallas", "los angeles" .....)
what need save in mongodb symfony2 mapping method. searched lot not able find proper or documentation regarding array handling in mongodb.
please explain if has experience in area.
namespace sptl\userbundle\document; use doctrine\odm\mongodb\mapping\annotations mongodb; /** * @mongodb\document */ class city { /** * @mongodb\id */ protected $id; /** * @mongodb\string */ protected $country; /** * @mongodb\hash */ protected $city = array(); /** * id * * @return id $id */ public function getid() { return $this->id; } /** * set country * * @param string $country * @return \city */ public function setcountry($country) { $this->country = $country; return $this; } /** * country * * @return string $country */ public function getcountry() { return $this->country; } /** * set city * * @param hash $city * @return \city */ public function setcity($city) { $this->city = $city; return $this; } /** * city * * @return hash $city */ public function getcity() { return $this->city; } }
usage insert:
$dm = $this->get('doctrine_mongodb')->getmanager(); $cities = new city(); $cities->setcountry("usa"); $cities->setcity(array("new york", "dallas", "los angeles")); $dm->persist($cities); $dm->flush(); inserted record:
{ "_id": objectid("5252b03f6b0f57394e2fb1b5"), "country": "usa", "city": { "0": "new york", "1": "dallas", "2": "los angeles" } } code used retrieve:
$repository = $this->get('doctrine_mongodb')->getmanager() ->getrepository('userbundle:city'); $cities = $repository->findby(array("_id"=>'5252b03f6b0f57394e2fb1b5')); print_r($cities); but not able record above retrieval code.....
if need retrieve city id of type objectid must create object mongoid , give id string
example:
$repository = $this->get('doctrine_mongodb')->getmanager() ->getrepository('userbundle:city'); $cityid = new \mongoid("5252b03f6b0f57394e2fb1b5"); $city = $repository->find($cityid); var_dump($city);
Comments
Post a Comment