title: CakePHP 3.x Contains GroupBy
body: <p>I am attempting to do a GroupBy on an associated table via contains -> conditions, however I am getting the following error...</p>
  
  <blockquote>
    <p>Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group = 'BrandUsers.user_id' AND BrandUsers.brand_id in (1,2,3,4,5,6))' at line 1</p>
  </blockquote>
  
  <p>with the following query</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT
      BrandUsers.id AS `BrandUsers__id`,
      BrandUsers.user_id AS `BrandUsers__user_id`,
      BrandUsers.brand_id AS `BrandUsers__brand_id`,
      Users.id AS `Users__id`,
      Users.username AS `Users__username`,
      Users.email AS `Users__email`,
      Users.password AS `Users__password`,
      Users.first_name AS `Users__first_name`,
      Users.last_name AS `Users__last_name`,
      Users.token AS `Users__token`,
      Users.token_expires AS `Users__token_expires`,
      Users.api_token AS `Users__api_token`,
      Users.activation_date AS `Users__activation_date`,
      Users.secret AS `Users__secret`,
      Users.secret_verified AS `Users__secret_verified`,
      Users.tos_date AS `Users__tos_date`,
      Users.active AS `Users__active`,
      Users.is_superuser AS `Users__is_superuser`,
      Users.role AS `Users__role`,
      Users.created AS `Users__created`,
      Users.modified AS `Users__modified` 
  FROM
      brand_users BrandUsers 
      INNER JOIN
          users Users 
          ON Users.id = 
          (
              BrandUsers.user_id
          )
  WHERE
      (
          group = :c0 
          AND BrandUsers.brand_id in 
          (
              :c1,
              :c2,
              :c3,
              :c4,
              :c5,
              :c6
          )
      )
  </code></pre>
  
  <p>I have taken a look at the following links, but the above error persists</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/37386189/group-by-within-contain-cakephp">Group By within contain cakephp</a></li>
  <li><a href="https://stackoverflow.com/questions/7970873/cakephp-group-by-within-containable">cakephp GROUP BY within containable</a></li>
  </ul>
  
  <p>Here is my code</p>
  
  <pre><code>$this-&gt;paginate = [
      'contain' =&gt; [
          'BrandUsers' =&gt; [
              'conditions' =&gt; [
                  'group' =&gt; 'BrandUsers.user_id'
              ]
          ],
          'BrandUsers.Users'
      ]
  ];
  
  $brands = $this-&gt;paginate(
      $this-&gt;Brands
          -&gt;find('all')
          -&gt;where(['Brands.user_id' =&gt; $this-&gt;Auth-&gt;user('id')])
  );
  </code></pre>