Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6707771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:40:44+00:00 2026-05-26T07:40:44+00:00

This is my first question on stackoverflow so be nice if i’m doing some

  • 0

This is my first question on stackoverflow so be nice if i’m doing some mistakes. Here’s my problem :

  • I’m working on a messaging system and in my inbox function i want to list every conversations owned by the current logged in user. This is not a problem but then i want, for each conversation, list every users that owns it too (= recipients).

Here is how i’m dealing with it :

    function inbox()
    {
      $conversationIDs = $this->ConversationUser->find('list', array(
           'fields' => array('ConversationUser.conversation_id'),
           'conditions' => array('user_id' => $this->Session->read('Auth.User.id')),
           'recursive' => -1
       ));


      $i = 0;
      foreach($conversationIDs as $conversation)
      {
          $array = $this->ConversationUser->find('first', array(
              'fields' => array('ConversationUser.conversation_id', 'ConversationUser.user_id', 'Conversation.subject', 'User.username'),
              'conditions' => array('ConversationUser.conversation_id' => $conversation, 
                  'NOT' => array('ConversationUser.user_id' => $this->Session->read('Auth.User.id'))
              ),
              'recursive' => 1
          ));
          $result[$i] = $array;
          $i++;
          $this->set(compact('result'));
      }
    }

I’m gathering every conversation’s id that the current user owns then “foreach” theses ids to find every users in these conversations.

Actually it is working very fine but i find the solution a bit dirty. Is there a better way to foreach in controller then pass this in view better than this ?

Or maybe my approach is very bad and i should do in a completely other way ?

Edit :

In my Conversation Model :

    public function getConversations($userID)
    { 
        $conversationsIDs = $this->ConversationUser->find('list', array(
            'fields' => array('ConversationUser.conversation_id'),
            'conditions' => array('ConversationUser.user_id' => $userID),
            'recursive' => -1,
       ));

        $conversations = $this->find('all', array(
            'conditions' => array(
                'Conversation.id IN('.implode(",", $conversationsIDs).')'
            ),
            'contain' => array(
                'LastMessage' => array(
                    'User' => array(
                        'fields' => array('User.username')
                    )
                ),
                'ConversationUser' => array(
                    'User' => array(
                        'fields' => array('User.username')
                    )
                )
            )
        ));
        return $conversations;
    }

In my Conversation controller :

    function inbox()
    {
        $conversations = $this->Conversation->getConversations($this->Session->read('Auth.User.id'));
        $this->set(compact("conversations"));
    }

What do you think about it ? Actually it’s working really fine for me. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:40:45+00:00Added an answer on May 26, 2026 at 7:40 am

    The problem about your approach is that it will invoke N+1 queries, where N is the number of conversations. The first query will retrieve the conversation ids, and then for every conversation it will retrieve recipients. You can optimize this by one of the following:

    1. Use Join to retrieve all the conversations and their recipients all at once: http://book.cakephp.org/view/1047/Joining-tables
    2. Use WHERE ConversationUser.user_id IN (id1,id2,id3) sql condition in the second query to retrieve the recipients for all the conversations in a single query.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question here @stackoverflow. I'm writing a monitoring tool for some
This is my first StackOverflow question so be nice! :-) I recently came across
this is my first question to stackoverflow so here it goes... I use cruise
Greetings to all! This is my first question here on stackoverflow. I have a
Hey stackoverflow - This is my first question here. I have 2 tables in
Since this is my first question here on stackoverflow I hope my question is
This is my first stackoverflow question, so try to be nice. ;-D My issue
This is my first question here on stackoverflow, so I hope that I am
This is my first question here on StackOverflow, and quite frankly I'm fairly new
this is my first question here in stackoverflow. I have two processes, a root

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.