3

I am new to the ORM but already impressed about its possibilities.

When designing Facebook-like messaging system (message threads allowing multiple user chat) I have come to a problem which I don't know how to solve.

After I load the MessageThread with DocumentManager::find(...) I get the whole MessageThread object with all the messages. This might not be a good idea because of memory limitations.

So my question is whether there is a way, how to load messages dynamically, on-demand so I get the MessageThread object but when accessing the messages property, they get loaded dynamically perhaps in bundle of 50 messages?

Thank you.

These are User, Message and MessageThread classes.

Using Doctrine MongoDB ODM

class User {
    /** @Id */
    protected $id;  
}

class Message
{
    /** @Id */
    protected $id;

    /** @ReferenceOne(targetDocument="User") */
    protected $sender;

    /** @String */
    protected $body;

    /** @Date */
    protected $sent;

    /** @EmbedMany */
    protected $read;
}


class MessageThread
{
     /** @Id */
     protected $id;

     ...

     /** @ReferenceMany(targetDocument="User") */
     protected $participants;

     //Maybe EmbedMany is better in this case
     /**
     * @ReferenceMany(
     *      targetDocument="Message",
     * )
     */
     protected $messages;

     ...
}
  • Are you sure the messages aren't loaded dynamically? It should so: when you start to iterate on the $messages array, Doctrine fetches the first batch of messages. However as you continue to cycle the $messages, they all remain in memory, so if you want to free the ram you'll have to call $odm->clear() – Madarco Dec 05 '13 at 08:31

0 Answers0