title: CakePHP 3: How to display associated data for each record in index.ctp?
body: <p>I have two tables <code>artists</code> and <code>pictures</code> with a <code>artist hasMany paintings</code> relation.</p>
  
  <p>Instead of having a separate view.ctp for every artist that displays the related pictures.I just want to display everything in the index.ctp file all artists listed with each ones associated pictures (list of IDs). </p>
  
  <p>After reading the cookbook and a bazillion threads I'm totally confused and don't understand how I have to do that. (I'm still very new to PHP and Cakephp)</p>
  
  <p>In the <code>view( )</code> function for the single artist I can get the related pictures easily like this:</p>
  
  <p><strong>Controller : ArtistsController.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>..
  public function view($id=null)
  {
     $artist = $this-&gt;Artists-&gt;get($id, [
        'contain' =&gt; ['Paintings']
     ])
  ...
  }
  </code></pre>
  
  <p>And then get everything in the corresponding <strong>view.ctp</strong> by doing: </p>
  
  <pre class="lang-php prettyprint-override"><code>view.ctp
  &lt;?php foreach ($artist-&gt;paintings as $paintings): ?&gt;
  &lt;ul&gt;&lt;li&gt; &lt;?= h($paintings-&gt;id) ?&gt; &lt;/li&gt;&lt;/ul&gt;
  ....
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>But how do I go about doing something similar for the <strong>index.ctp</strong> / index() function for each artist listed in the table? So I can essentially have sth like this:</p>
  
  <pre><code>&lt;?php foreach ($artists as $artist): ?&gt;
      &lt;tr&gt;&lt;td&gt; &lt;?= h($artist-&gt;name) ?&gt; &lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt; &lt;?php foreach ($artist-&gt;paintings as $paintings): ?&gt;
               &lt;ul&gt;&lt;li&gt; &lt;?= h($painting-&gt;id) ?&gt; &lt;/li&gt;&lt;/ul&gt;
               &lt;?php endforeach; ?&gt;
      &lt;/tr&gt;&lt;/td&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>I know it's not really working like that but how would I go about doing it? 
  Do I have to use a find() query in the Controllers index() function and store the results before retrieving it in the view?! I did try that but it either displayed nothing or it displayed an SQL query itself because I messed up the syntax somehow...</p>
  
  <p>So after trying various things from the book and searching for hours it seems I am missing some basic understanding in my brain here. Also all similar questions I found lead me to nothing.</p>
  
  <p>Even a hint where to start with this would be great! Thanks a lot!</p>