title: CakePHP 3 join table associations
body: <p>Alright, I have some issues understanding how the associations are working, particularly <strong>belongsTo</strong> here is my setup:</p>
  
  <p>Articles can have multiple Categories<br>
  Categories can belong to multiple Articles</p>
  
  <p>so in my database i have 3 tables:
  <code>articles</code>, <code>categories</code> and a join table <code>articles_categories</code></p>
  
  <p><strong>Table/ArticlesTable.php:</strong></p>
  
  <pre><code>public function initialize(array $config)
  {
      $this-&gt;addBehavior('Timestamp');
      $this-&gt;table('articles');
      $this-&gt;belongsTo('Users');
      $this-&gt;belongsToMany('Categories', [
          'through' =&gt; 'ArticlesCategories',
          'alias' =&gt; 'Categories',
          'foreignKey' =&gt; 'article_id',
          'joinTable' =&gt; 'articles_categories',
          'targetForeignKey' =&gt; 'category_id'
  
      ]);
  
  }
  </code></pre>
  
  <p><strong>Table/CategoriesTable.php:</strong></p>
  
  <pre><code>public function initialize(array $config)
  {
      $this-&gt;table('categories');
      $this-&gt;displayField('name');
      $this-&gt;primaryKey('id');
      $this-&gt;addBehavior('Timestamp');
      $this-&gt;belongsToMany('Articles', [
          'through' =&gt; 'ArticlesCategories',
          'alias' =&gt; 'Articles',
          'foreignKey' =&gt; 'category_id',
          'joinTable' =&gt; 'articles_categories',
          'targetForeignKey' =&gt; 'article_id'
      ]);
  }
  </code></pre>
  
  <p><strong>Table/ArticlesCategoriesTable.php:</strong></p>
  
  <pre><code>public function initialize(array $config)
  {
      $this-&gt;belongsTo('Articles');
      $this-&gt;belongsTo('Categories');
  }
  </code></pre>
  
  <p>Now inside in the <code>view</code> action CategoriesController.php i can overview a particular category and i need to retrieve some articles related to that category.
  <strong>What is the right way to do such a thing?</strong>  Here is what i have:</p>
  
  <pre><code>public function view($id = null)
  {
      $category = $this-&gt;Categories-&gt;find('all',['limit'=&gt;1])-&gt;where(['Categories.id' =&gt; $id])-&gt;contain(['Articles']);
      $this-&gt;set(['category'=&gt; $category]);
  }
  </code></pre>
  
  <p>It kinda does the job but I'd also need to be able to limit the number of related articles..</p>