如何检索 Propel 1.6 中每个线程的最新问题?

Posted

技术标签:

【中文标题】如何检索 Propel 1.6 中每个线程的最新问题?【英文标题】:How can I retrieve the latest question of each thread in Propel 1.6? 【发布时间】:2011-09-13 17:10:13 【问题描述】:

我想通过 Propel 1.6 使用 fluid ModelQuery 接口为我的每个线程(私人消息系统)获取最新条目。这将允许我重用这两种方法来获取最新条目,并且只获取涉及用户的条目(没有人希望看到不属于他的消息)。

我已经发现,在标准 SQL 中,我必须为我的每个论坛线程使用 get the newest entry 的子查询。我还发现在 Propel 中你必须使用 Criteria::CUSTOM 查询来实现这一点,但整个 Criteria::CUSTOM 的东西似乎是 Propel-1.6 之前的,因为没有一个示例使用新的 ModelQuery。

现在的问题是,我想利用 ModelQueries 中的连接功能,您可以像这样将几个自己的方法相互附加:

$entries = MessageQuery::create()
     ->messagesInvolvingUser($user) // user retrieved or sent the message
     ->newestFromThread() // get the latest entry from a lot of Re:-stuff

如果我不得不使用,我认为这仍然是可能的

$c = new Criteria();
$c->add([the subquery filter]);

newestFromThread().

在给定以下方案的情况下,检索每个线程的最新条目的最佳方法是什么(thread_id 表示所有消息属于同一个对应关系,我只希望每个 thread_id 一个条目):

id(INT)
title(VARCHAR)
thread_id(INTEGER)
date(DATETIME)

当前的 php 实现如下所示:

<?php

class MessageQuery extends BaseMessageQuery 
    public function messagesInvolvingUser($user) 
        return $this
            ->where('Message.AuthorId = ?', $user->getId())
            ->_or()
            ->where('Message.RecipientId = ?', $user->getId());
    

    public function newestFromThread() 
        return $this;
        // To be implemented
    

我是这样使用它的:

$messages = MessageQuery::create()
    ->messagesInvolvingUser(Zend_Auth::getInstance()->getIdentity())
    ->newestFromThread()
    ->find();

【问题讨论】:

【参考方案1】:

按日期排序结果 (DESC) 并限制为一个结果如何?

【讨论】:

【参考方案2】:

考虑到关于pure SQL solutions 的类似问题的答案,我想最简单的方法是添加一个新列newest,指示通信中的哪条消息是最新的。这可能也更适合 Propel 的面向对象方法。我可以这样写我的应用程序:

public function preInsert(PropelPDO $con = null) 
    $this->setNewest(1);
    $this->getEarlier()->setNewest(0);

    return true;

【讨论】:

以上是关于如何检索 Propel 1.6 中每个线程的最新问题?的主要内容,如果未能解决你的问题,请参考以下文章

填充对象时覆盖 Propel 1.6 构造函数会引发警告

SQL如何从每个连接表中检索最新结果

SQL 检索最新记录,按唯一外键分组

推进 - 检索所有表

php(propel)中的集合和数组有啥区别?

如何获取相关对象 Propel ORM