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

发布于 2024-12-04 04:16:09 字数 1940 浏览 1 评论 0原文

我想通过 Propel 1.6 利用流畅的 ModelQuery 界面获取每个线程(私人消息系统)的最新条目。这将使我能够重用两种方法来获取最新条目,并且只获取涉及用户的条目(没有人希望看到不适合他的消息)。

我已经发现在标准 SQL 中我必须使用子查询来 获取最新条目 我的每个论坛主题。我还发现在 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]);

中 使用,这仍然是可能的最新的FromThread()。

给定以下方案检索每个线程的最新条目的最佳方法是什么(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();

I want to get the newest entries for each of my threads (private messaging system) with Propel 1.6 making use of the fluid ModelQuery interface. This would allow me to reuse both methods for getting newest entries and only getting entries where a user is involved (nobody wants to see messages not for him).

I already found out that in standard-SQL I have to use a subquery to get the newest entry for each of my forum threads. I also found out that in Propel you have to use a Criteria::CUSTOM query to achieve this, but the whole Criteria::CUSTOM stuff seems to be pre-Propel-1.6, because none of the examples makes use of the new ModelQuery.

Now the problem is, that I want to make use of the concenation feature in ModelQueries, where you can attach several own methods to each other like this:

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

I do not think that this would still be possible if I had to use

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

in newestFromThread().

What’s the best method to retrieve the latest entry for each thread given the following scheme (thread_id means that all messages belong to the same correspondence, I want only one entry per thread_id):

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

The current PHP-implementation looks like this:

<?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
    }
}

And I am using it like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝梦月影 2024-12-11 04:16:09

按日期 (DESC) 对结果排序并限制为一个结果怎么样?

How about ordering results by date (DESC) and to limit to one result ?

烦人精 2024-12-11 04:16:09

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

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

    return true;
}

Considering the answers in a similar question about pure SQL solutions, I guess it is easiest to add a new column newest indicating which message in a communcation is the newest. This probably fits the object-oriented approach of Propel better, too. I could write my application like this then:

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

    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文