2011-05-30 29 views
2

我遇到了Symfony2.0 BETA3和MongoDB的問題。我想創建一個文檔,其中一個字段參考到另一個類,這可能是這樣的:ReferenceOne與MongoDB

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'. 

UPDATE

Here is a Document

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 
)  

我不知道這個班級有什麼問題。可以請別人幫忙嗎?

在此先感謝!

  • 蒙蒂

回答

3

如果你想在你需要使用的,而不是「referenceOne」 embedOne'的任何位置字段進行搜索。 ReferenceOne沒有位置字段複製到主文件,它只是數據是這樣的(我不記得確切):

{ 
refId: '1', 
refColl: 'locations', 
refDb: 'location_database' 
} 

但總的來說,如果你只需要通過位置ID查詢,你只需要看看如何定位引用看起來像在mongodb中使用mongoshell或其他工具。

所以您查詢將是這樣的:

->field('location.$refId')->equals($locationID) 
+0

嗨安德魯,感謝您的回答,我添加了一個文件,它的樣子,我真的應該使用$ ID,而不是$ REFID。 – mrohnstock 2011-05-30 12:51:15

+0

與embedOne或referenceOne相同的問題,找不到位置映射。 – mrohnstock 2011-05-30 13:00:50

+0

@Monty我想你需要重新生成一個代理應用緩存下的水化器 - > http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/console-commands.html – 2011-05-30 15:24:47