CakePHP消息系统

发布于 2024-12-24 21:53:57 字数 1151 浏览 0 评论 0原文

我正在尝试在 CakePHP 1.3 中实现一个简单的消息系统。我需要做的是执行查询以返回我的登录用户正在与之对话的用户数组(因此要么收到消息,要么向其发送消息,要么两者都发送)。

我的用户模型的设置如下:

class User extends AppModel {
    var $name = 'User';
    var $actsAs = array('Containable');

    var $hasMany = array(

        'SentMessages' =>
        array (
            'className' => 'Message',
            'foreignKey' => 'sender_id'
        ),

        'ReceivedMessages' =>
        array (
            'className' => 'Message',
            'foreignKey' => 'receiver_id'
        )
    );  
}

我可以使用 Containable 仅检索用户及其相关消息,并向这些 Contain 语句添加条件,以将这些消息限制为每个用户和我登录的用户之间的消息。

$users = $this->User->find('all', array(
    'contain' => array(
        'ReceivedMessages' => array(
            'conditions' => array(
                'receiver_id' => $my_user_id
            )
        ),
        'SentMessages' => array(
            'conditions' => array(
                'sender_id' => $my_user_id
            )
        )
    )
));

但是,这仍然返回我的数据库中的每个用户。有没有办法编写此查询,以便仅返回具有任何 SentMessages 或 ReceivedMessages 的用户?

谢谢!

I'm trying to implement a simple messaging system in CakePHP 1.3. What I need to do is perform a query to return an array of users that my logged in user is having a conversation with (so has either received a message from, sent a message to, or both).

My User model is set up like so:

class User extends AppModel {
    var $name = 'User';
    var $actsAs = array('Containable');

    var $hasMany = array(

        'SentMessages' =>
        array (
            'className' => 'Message',
            'foreignKey' => 'sender_id'
        ),

        'ReceivedMessages' =>
        array (
            'className' => 'Message',
            'foreignKey' => 'receiver_id'
        )
    );  
}

I can use Containable to retrieve just the User and their related messages, and add conditions to those Contain statements to limit those messages to the ones between each user and my logged in user.

$users = $this->User->find('all', array(
    'contain' => array(
        'ReceivedMessages' => array(
            'conditions' => array(
                'receiver_id' => $my_user_id
            )
        ),
        'SentMessages' => array(
            'conditions' => array(
                'sender_id' => $my_user_id
            )
        )
    )
));

However, this still returns EVERY user in my DB. Is there a way to write this query so that only the Users with any SentMessages or ReceivedMessages are returned?

Thanks!

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

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

发布评论

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

评论(2

月野兔 2024-12-31 21:53:57
$users = $this->User->find('first', array(
    'conditions' => array('User.id' => $my_user_id),
    'contain' => array(
        'ReceivedMessages' => array(
            'conditions' => array(
                'receiver_id' => $my_user_id
            )
        ),
        'SentMessages' => array(
            'conditions' => array(
                'sender_id' => $my_user_id
            )
        )
    )
));

我的推理是,在您的原始代码中,您正在查找所有,但没有指定查找的条件。

然后,Cake 将返回所有用户及其存在的任何消息。我希望 $users 数组包含正确格式的正确数据,但您只需要特定的用户。

通过将条件添加到初始查找中,您应该可以开始了。

$users = $this->User->find('first', array(
    'conditions' => array('User.id' => $my_user_id),
    'contain' => array(
        'ReceivedMessages' => array(
            'conditions' => array(
                'receiver_id' => $my_user_id
            )
        ),
        'SentMessages' => array(
            'conditions' => array(
                'sender_id' => $my_user_id
            )
        )
    )
));

My reasoning is in your original code you are finding all but not specifying a condition for that find.

Cake is then returning ALL your users and any messages where they exist. I expect the $users array contained the correct data in the correct format, but you only require a specific users.

By adding the condition to the initial find you should be good to go.

勿忘初心 2024-12-31 21:53:57

我通过添加一个新的“对话”实体解决了这个问题,如下所示:

var $hasMany = array(
    'Message' => array(
        'className' => 'Message',
        'foreignKey' => 'conversation_id'
    )
);

var $belongsTo = array(
    'FirstUser' => array(
        'className' => 'User',
        'foreignKey' => 'first_user_id'
    ),
    'SecondUser' => array(
        'className' => 'User',
        'foreignKey' => 'second_user_id'
    )
);

然后执行以下查找:

$conversations = $this->Conversation->find('all',array(
    'conditions' => array(
        'OR' => array(
            'Conversation.first_user_id' => $this->my_user_id,
            'Conversation.second_user_id' => $this->my_user_id
        ),
        'Conversation.modified >' => date("Y-m-d H:i:s", $since_timestamp)
    )
);

I solved this by adding a new 'Conversation' entity like so:

var $hasMany = array(
    'Message' => array(
        'className' => 'Message',
        'foreignKey' => 'conversation_id'
    )
);

var $belongsTo = array(
    'FirstUser' => array(
        'className' => 'User',
        'foreignKey' => 'first_user_id'
    ),
    'SecondUser' => array(
        'className' => 'User',
        'foreignKey' => 'second_user_id'
    )
);

Then performing the following find:

$conversations = $this->Conversation->find('all',array(
    'conditions' => array(
        'OR' => array(
            'Conversation.first_user_id' => $this->my_user_id,
            'Conversation.second_user_id' => $this->my_user_id
        ),
        'Conversation.modified >' => date("Y-m-d H:i:s", $since_timestamp)
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文