2012-03-20 38 views
2

我使用教義/ mongodb-odm-bundle,我有一個問題:我無法從文檔中獲取引用行(或者我不知道如何執行此操作。 。) 我有2個文件用一個到許多參考這樣的: 第一
學說2 MongoDB獲取對象的子記錄

/** 
* @MongoDB\Document(collection="categories") 
*/ 
class Category 
{ 
    /** 
    * @var integer $id 
    * 
    * @MongoDB\Id(strategy="auto") 
    */ 
    private $id; 

    /** 
    * @var string $name 
    * 
    * @MongoDB\String 
    * @Assert\NotBlank() 
    * @Assert\MinLength(3) 
    */ 
    private $name; 

    /** 
    * @MongoDB\ReferenceMany(targetDocument="Application\Bundle\DefaultBundle\Document\Wallpaper", mappedBy="category") 
    */ 
    private $files; 
................. 
    /** 
    * Set files 
    * 
    * @param array $files 
    */ 
    public function setFiles($files) 
    { 
     $this->files = $files; 
    } 

    /** 
    * Get files 
    * 
    * @return array $files 
    */ 
    public function getFiles() 
    { 
     return $this->files; 
    } 

.................
第二

/** 
* @MongoDB\Document(collection="wallpapers") 
*/ 
class Wallpaper 
{ 
    /** 
    * @var string $id 
    * @MongoDB\Id(strategy="auto") 
    */ 
    protected $id; 
    /** 
    * @MongoDB\ReferenceOne(targetDocument="Application\Bundle\DefaultBundle\Document\Category", inversedBy="files") 
    */ 
    private $category; 

    /** 
    * Get category 
    * 
    * @return Application\Bundle\DefaultBundle\Document\Category $category 
    */ 
    public function getCategory() 
    { 
     return $this->category; 
    } 

    /** 
    * Set category 
    * 
    * @param Application\Bundle\DefaultBundle\Document\Category $category 
    */ 
    public function setCategory($category) 
    { 
     $this->category = $category; 
    } 

} 

這是來自控制器的代碼:

$category = $dm->getRepository('ApplicationDefaultBundle:Category')->findOneBy(...); 
$wallpapers = $category->getFiles(); 

$ wallpapers和$ document-> files are NULL。我如何檢索與類別相關的記錄?以及如何從混凝土牆紙對象中獲取類別?是否有像標準ORM中的「JOIN」模擬?

回答

2

映射看起來正確。我認爲你的問題可能與查詢有關。我還會檢查壁紙集合是否具有正確的文檔,並在類別字段中填充正確的DBRef對象數據。

$category = $dm->getRepository('Application\Bundle\DefaultBundle\Document\Wallpaper')->findOneById($id); 
$wallpapers = $category->getFiles(); // Will return a cursor to the wallpaper objects 
foreach ($wallpapers as $wallpaper) { 
    do stuff 
} 

如果這不是問題,你能粘貼你想全面的查詢,並從兩個集合的數據樣本。

0

沒有像SQL那樣的「連接」,ODM將進行單獨的查詢並將它們組合到對象中。默認情況下,原則在訪問該部分時會很懶惰。

正如傑米所說,查詢和數據是幫助這裏的關鍵部分。

2

你確定DoctrineORM刪除了你的項目嗎?我有這個問題。我刪除了我的項目DoctrineORM,它的工作。