title: How to Filter Associated Data using FriendsOfCake Search plugin for CakePHP 3.1.0
body: <p>Plugin: <code>FriendsOfCake\Search</code></p>
  
  <pre><code>CakePHP: 3.1.0
  </code></pre>
  
  <p>I'm currently adding the ability to filter my Orders controller on the <code>index()</code> method. I'm needing to be able to search Orders my the name of the User who placed the order. Each order is associated with the <code>Users</code> model:</p>
  
  <pre><code>    $this-&gt;belongsTo('Users', [
          'foreignKey' =&gt; 'user_id',
          'joinType' =&gt; 'INNER'
      ]);
  </code></pre>
  
  <p>When I build my <code>searchConfiguration()</code> method in the <code>OrdersTable.php</code> file I have the following:</p>
  
  <pre><code>    -&gt;value('first_name', [
          'field' =&gt; $this-&gt;aliasField('Users.first_name')
      ])
      -&gt;value('last_name', [
          'field' =&gt; $this-&gt;aliasField('Users.last_name')
      ])
  </code></pre>
  
  <p>The Orders index() method </p>
  
  <pre><code>    $query = $this-&gt;Orders-&gt;find('search', 
          $this-&gt;Orders-&gt;filterParams($this-&gt;request-&gt;query))-&gt;contain(['Users', 'PaymentMethods', 'Industries']
      )-&gt;order(['Orders.created' =&gt; 'DESC']);
      $this-&gt;set('orders', $this-&gt;paginate($query));
  </code></pre>
  
  <p>This loads fine when I'm not passing any parameters to the query, however, as soon as I try and search by <code>first_name</code> or <code>last_name</code> I get the error:</p>
  
  <blockquote>
    <p>Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Orders.Users.first_name' in 'where clause'</p>
  </blockquote>
  
  <p>Based on the error, it's appending Orders. to the field that I'm trying to search by. From what I can tell <code>CakePHP</code> has 2 methods <code>aliasField()</code> one in <code>\var\www\&lt;project_name&gt;\vendors\cakephp\cakephp\src\ORM\Query.php</code> </p>
  
  <pre><code>public function aliasField($field, $alias = null)
  {
      $namespaced = strpos($field, '.') !== false;
      $aliasedField = $field;
  
      if ($namespaced) {
          list($alias, $field) = explode('.', $field);
      }
  
      if (!$alias) {
          $alias = $this-&gt;repository()-&gt;alias();
      }
  
      $key = sprintf('%s__%s', $alias, $field);
      if (!$namespaced) {
          $aliasedField = $alias . '.' . $field;
      }
  
      return [$key =&gt; $aliasedField];
  }
  </code></pre>
  
  <p>And one in <code>\var\www\&lt;project_name&gt;\vendors\cakephp\cakephp\src\ORM\Table.php</code></p>
  
  <pre><code>public function aliasField($field)
  {
      return $this-&gt;alias() . '.' . $field;
  }
  </code></pre>
  
  <p>It appears that <code>Query.php</code> will allow you to specify the <code>$alias</code> for the field, however, Table.php does not, so I assume that's the method that's being used here. </p>
  
  <p>Can anyone give me guidance on how to filter on data contained in an associated table?</p>