我遇到了Symfony2.0 BETA3和MongoDB的问题。我想创建一个Document,其中Field引用另一个Class,这可能如下所示:
namespace test\TestBundle\Document;
use test\TestBundle\Document\Location;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document(collection="locationcache", repositoryClass="test\TestBundle\Document\LocationCacheRepository")
*/
class LocationCache
{
// many more fields...
/**
* @var Location
* @ODM\ReferenceOne(targetDocument="test\TestBundle\Document\Location")
*/
protected $location;
/**
* @param Location
*/
public function setLocation(Location $location)
{
$this->location = $location;
}
/**
* @return Location
*/
public function getLocation()
{
return $this->location;
}
}
但如果我想通过$ id找到这样的位置
class LocationCacheRepository extends DocumentRepository
{
public function findByLocationID(MongoId $locationID)
{
return $this->createQueryBuilder()
->field('location.$id')->equals($locationID)
->sort('year', 'asc')
->sort('month', 'asc')
->getQuery()
->execute();
}
}
我会收到此错误
No mapping found for field 'location' in class 'test\TestBundle\Document\LocationCache'.
更新
这是一份文件
Array
(
[_id] => 4dd637e706936bbcc0ac012d
[days] => Array
(
[1] => Array
(
[money] => 9
)
[2] => Array
(
[money] => 21
)
[3] => Array
(
[money] => 38
)
[4] => Array
(
[money] => 6
)
[18] => Array
(
[money] => 6
)
[19] => Array
(
[money] => 3
)
[31] => Array
(
[money] => 11
)
)
[location] => Array
(
[$ref] => location
[$id] => 4dd554c91c911a6606000000
[$db] => test
)
[money] => 94
[month] => 1
[year] => 2011
)
我不知道课程有什么问题。可以请别人帮忙吗?
提前致谢!
答案 0 :(得分:3)
如果要搜索任何位置字段,则需要使用“embedOne”而不是“referenceOne”。 ReferenceOne不会将位置字段复制到父文档中,它只是这样的数据(我不记得确切):
{
refId: '1',
refColl: 'locations',
refDb: 'location_database'
}
但一般情况下,如果您只需要按位置ID进行查询,则只需使用mongoshell或其他工具查看位置参考在mongodb中的样子。
所以你的查询将是这样的:
->field('location.$refId')->equals($locationID)