title: Cakephp - Foreach in a controller, how to properly pass the result to view?
body: <p>This is my first question on stackoverflow so be nice if i'm doing some mistakes. Here's my problem :</p>
  
  <ul>
  <li>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).</li>
  </ul>
  
  <p>Here is how i'm dealing with it :</p>
  
  <pre><code>    function inbox()
      {
        $conversationIDs = $this-&gt;ConversationUser-&gt;find('list', array(
             'fields' =&gt; array('ConversationUser.conversation_id'),
             'conditions' =&gt; array('user_id' =&gt; $this-&gt;Session-&gt;read('Auth.User.id')),
             'recursive' =&gt; -1
         ));
  
  
        $i = 0;
        foreach($conversationIDs as $conversation)
        {
            $array = $this-&gt;ConversationUser-&gt;find('first', array(
                'fields' =&gt; array('ConversationUser.conversation_id', 'ConversationUser.user_id', 'Conversation.subject', 'User.username'),
                'conditions' =&gt; array('ConversationUser.conversation_id' =&gt; $conversation, 
                    'NOT' =&gt; array('ConversationUser.user_id' =&gt; $this-&gt;Session-&gt;read('Auth.User.id'))
                ),
                'recursive' =&gt; 1
            ));
            $result[$i] = $array;
            $i++;
            $this-&gt;set(compact('result'));
        }
      }
  </code></pre>
  
  <p>I'm gathering every conversation's id that the current user owns then "foreach" theses ids to find every users in these conversations.</p>
  
  <p>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 ?</p>
  
  <p>Or maybe my approach is very bad and i should do in a completely other way ?</p>
  
  <h2>Edit :</h2>
  
  <p>In my Conversation Model :</p>
  
  <pre><code>    public function getConversations($userID)
      { 
          $conversationsIDs = $this-&gt;ConversationUser-&gt;find('list', array(
              'fields' =&gt; array('ConversationUser.conversation_id'),
              'conditions' =&gt; array('ConversationUser.user_id' =&gt; $userID),
              'recursive' =&gt; -1,
         ));
  
          $conversations = $this-&gt;find('all', array(
              'conditions' =&gt; array(
                  'Conversation.id IN('.implode(",", $conversationsIDs).')'
              ),
              'contain' =&gt; array(
                  'LastMessage' =&gt; array(
                      'User' =&gt; array(
                          'fields' =&gt; array('User.username')
                      )
                  ),
                  'ConversationUser' =&gt; array(
                      'User' =&gt; array(
                          'fields' =&gt; array('User.username')
                      )
                  )
              )
          ));
          return $conversations;
      }
  </code></pre>
  
  <p>In my Conversation controller :</p>
  
  <pre><code>    function inbox()
      {
          $conversations = $this-&gt;Conversation-&gt;getConversations($this-&gt;Session-&gt;read('Auth.User.id'));
          $this-&gt;set(compact("conversations"));
      }
  </code></pre>
  
  <p>What do you think about it ? Actually it's working really fine for me. :)</p>