Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按需加载引用任意对象_Php_Mongodb_Doctrine_Doctrine Orm_Odm - Fatal编程技术网

Php 按需加载引用任意对象

Php 按需加载引用任意对象,php,mongodb,doctrine,doctrine-orm,odm,Php,Mongodb,Doctrine,Doctrine Orm,Odm,我对ORM还不熟悉,但已经对它的可能性印象深刻 在设计类似Facebook的消息系统(允许多用户聊天的消息线程)时,我遇到了一个我不知道如何解决的问题 在我用DocumentManager::find(…)加载MessageThread之后,我得到了包含所有消息的整个MessageThread对象。由于内存限制,这可能不是一个好主意 所以我的问题是,是否有一种方法,如何按需动态加载消息,这样我就得到了MessageThread对象,但是当访问messages属性时,它们会动态加载,可能是以50条

我对ORM还不熟悉,但已经对它的可能性印象深刻

在设计类似Facebook的消息系统(允许多用户聊天的消息线程)时,我遇到了一个我不知道如何解决的问题

在我用DocumentManager::find(…)加载MessageThread之后,我得到了包含所有消息的整个MessageThread对象。由于内存限制,这可能不是一个好主意

所以我的问题是,是否有一种方法,如何按需动态加载消息,这样我就得到了MessageThread对象,但是当访问messages属性时,它们会动态加载,可能是以50条消息的形式

多谢各位

这些是用户、消息和消息线程类

使用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;

     ...
}

您确定没有动态加载邮件吗?应该这样做:当您开始迭代$messages数组时,条件性会获取第一批消息。但是,当您继续循环$messages时,它们都会保留在内存中,因此如果您想释放ram,您必须调用$odm->clear()