【发布时间】:2013-10-14 02:18:50
【问题描述】:
我必须在城市级别进行自动建议,以便我列出与各个国家/地区映射的城市,例如 USA => ( "New York", "Dallas", "Los Angeles" .....)
我需要的是使用 symfony2 映射方法将其保存在 mongodb 中。 我进行了很多搜索,但找不到有关 mongodb 中数组处理的适当帮助或文档。
如果有人在这方面有经验,请说明。
namespace SPTL\UserBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document
*/
class City {
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\String
*/
protected $country;
/**
* @MongoDB\hash
*/
protected $city = array();
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get city
*
* @return hash $city
*/
public function getCity()
{
return $this->city;
}
}
插入用法:
$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();
插入记录:
{
"_id": ObjectId("5252b03f6b0f57394e2fb1b5"),
"country": "USA",
"city": {
"0": "New York",
"1": "Dallas",
"2": "Los Angeles"
}
}
用于检索的代码:
$repository = $this->get('doctrine_mongodb')->getManager()
->getRepository('UserBundle:City');
$cities = $repository->findBy(array("_id"=>'5252b03f6b0f57394e2fb1b5'));
print_r($cities);
但我无法通过上述检索代码获取记录.....
【问题讨论】: