title: CakePHP 3 - association is not defined - even though it appears to be
body: <p>I've baked a legacy application using CakePHP 3.7</p>
  
  <p>The database contains 3 tables as follows: <code>regulations</code>, <code>groups</code>, <code>filters</code>. The hierarchy as far as the application goes is:</p>
  
  <ul>
  <li>Regulations
  
  <ul>
  <li>Groups
  
  <ul>
  <li>Filters </li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>The table schemas are as follows:</p>
  
  <pre><code>mysql&gt; describe regulations;
  +-------+---------------------+------+-----+---------+----------------+
  | Field | Type                | Null | Key | Default | Extra          |
  +-------+---------------------+------+-----+---------+----------------+
  | id    | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
  | name  | varchar(255)        | NO   |     | NULL    |                |
  | label | varchar(255)        | NO   |     | NULL    |                |
  +-------+---------------------+------+-----+---------+----------------+
  4 rows in set (0.09 sec)
  
  mysql&gt; describe groups;
  +---------------+-----------------+------+-----+---------+----------------+
  | Field         | Type            | Null | Key | Default | Extra          |
  +---------------+-----------------+------+-----+---------+----------------+
  | id            | int(4) unsigned | NO   | PRI | NULL    | auto_increment |
  | name          | varchar(255)    | NO   |     | NULL    |                |
  | regulation_id | int(4) unsigned | NO   | MUL | NULL    |                |
  | label         | varchar(255)    | NO   |     | NULL    |                |
  +---------------+-----------------+------+-----+---------+----------------+
  4 rows in set (0.03 sec)
  
  mysql&gt; describe filters;
  +----------+----------------------+------+-----+---------+----------------+
  | Field    | Type                 | Null | Key | Default | Extra          |
  +----------+----------------------+------+-----+---------+----------------+
  | id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
  | name     | varchar(100)         | NO   |     | NULL    |                |
  | label    | text                 | NO   |     | NULL    |                |
  | group_id | int(4) unsigned      | NO   | MUL | NULL    |                |
  +----------+----------------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>So when I baked the application I got the following Table class relationships. All are inside the <code>initialize()</code> method:</p>
  
  <pre><code>// RegulationsTable.php
  $this-&gt;hasMany('Groups', [
          'foreignKey' =&gt; 'regulation_id'
      ]);
  
  // GroupsTable.php
  $this-&gt;belongsTo('Regulations', [
          'foreignKey' =&gt; 'regulation_id',
          'joinType' =&gt; 'INNER'
      ]);
      $this-&gt;hasMany('Filters', [
          'foreignKey' =&gt; 'group_id'
      ]);
  
   // FiltersTable.php
   $this-&gt;belongsTo('Groups', [
          'foreignKey' =&gt; 'group_id',
          'joinType' =&gt; 'INNER'
      ]);
  </code></pre>
  
  <p>In my mind this seems correct because those are the relationships as far as the schema goes.</p>
  
  <p>However, when I try and do this in a Controller:</p>
  
  <pre><code>$regulations = TableRegistry::getTableLocator()-&gt;get('Regulations');
  $data = $regulations-&gt;find('all')-&gt;contain(['Groups', 'Filters']);
  $data = $data-&gt;toArray();
  debug($data);
  </code></pre>
  
  <p>It's giving an error:</p>
  
  <blockquote>
    <p>The Filters association is not defined on Regulations.</p>
  </blockquote>
  
  <p>Well, obviously. Because the association for that is handled through the Groups (<code>GroupsTable.php</code>) model. There is no <b>direct</b> relationship between Filters and Regulations; it must go via Groups.</p>
  
  <p>Please can someone advise what is wrong with the baked Models so that this won't produce an error? </p>
  
  <p>For background info: the use case is getting a list of the data (specifically the <code>label</code> from each table) in a hierarchical format (Regulations --> Groups --> Filters).</p>