Find by conditions on associated model in CakePHP 3
<p>I have two tables <code>orders</code> and <code>sub_orders</code>.
Their association is</p>

<pre><code>$orders-&gt;hasMany('SubOrders', [
   'foreignKey' =&gt; 'order_id'
]);
</code></pre>

<p>Both tables have <code>invoice_no</code> and <code>sub_invoice</code> columns in <code>orders</code> and <code>sub_orders</code> respectively.</p>

<p>I have to find records from <code>orders</code> table containing associated <code>sub_orders</code> where <code>$trackingId</code> will match either <code>Orders.invoice_no</code> or <code>SubOrders.sub_invoice</code></p>

<pre><code>$findOrder = $this-&gt;Orders-&gt;find('all', [
    'conditions' =&gt; [
      'OR' =&gt; [
         'Orders.invoice_no' =&gt; $trackingId,
         'SubOrders.sub_invoice' =&gt; $trackingId
       ]
     ],
     'contain' =&gt; [
        'SubOrders'
     ]
  ]);
</code></pre>

<p>But this gives error</p>

<pre><code>Column not found: 1054 Unknown column 'SubOrders.sub_invoice' in 'where clause'
</code></pre>