2013-03-02 41 views
1

我無法在doctrine手冊中找到如何執行非常簡單的查詢。我不知道如何管理與MongoDb相同的SQL「JOIN ...」。在ReferenceMany字段中查找對象數組內的特定對象

abstract class Topic 
{ 
    /** 
    * @MongoDB\Id 
    */ 
    protected $id; 
} 

abstract class Message 
{ 
    /** 
    * @MongoDB\Id 
    */ 
    protected $id; 

    /** 
    * @MongoDB\Date 
    */ 
    protected $date; 

    /** 
    * @MongoDB\ReferenceOne(targetDocument="Topic") 
    */ 
    protected $topic; 
} 

abstract class User 
{ 
    /** 
    * @MongoDB\Id 
    */ 
    protected $id; 

    /** 
    * @MongoDB\ReferenceMany(targetDocument="Message") 
    */ 
    protected $messages; 
} 

我有一個用戶ID和一個主題ID。 我想:
1)查找用戶
2)最近的消息查找有關該用戶的話題的最新消息

$ DM->找到( '用戶',$ ID) - > getMessages()給了我一個「PersistentCollection」
$ dm-> find('User',$ id) - > getMessages() - > getValues()給了我消息的數組,然後我必須使用PHP循環,我敢肯定有一種方法來創建一個查詢來做到這一點...

我應該使用地圖還是減少?

有人可以幫助我嗎?謝謝 !

回答

1

首先,你必須確定你的數據庫模式:

一個消息可以屬於多個用戶?

通常一個消息只能有一個作家,所以我會更改消息文件存儲筆者User id

class Message 
{ 
    /** 
    * @MongoDB\ReferenceOne(targetDocument="User") 
    */ 
    protected $author; 

    //Othe methods... 
} 

然後從用戶查找最新消息:

class User { 

    /** 
    * @ReferenceOne(
    *  targetDocument="Message", 
    *  mappedBy="author", 
    *  sort={"date"="desc"} 
    *) 
    */ 
    protected $lastMessage; 

    //Other methods... 
} 

並找到關於一個話題的最新消息:在

class User { 

    /** 
    * @ReferenceOne(
    *  targetDocument="Message", 
    *  mappedBy="author", 
    *  criteria={"topic" : "sport"} 
    *  sort={"date"="desc"} 
    *) 
    */ 
    protected $lastMessageAboutSport 

    //Other methods... 
} 

更多信息:Doctrine ODM Complex References Guide

+0

謝謝,我不知道那沉重的註釋是答案。 – 2013-05-03 23:05:23

相關問題