title: Lithium count with left join
body: <p>As part of a larger query with dynamically-generated elements, I need to do a left join and count. This is the query I need.</p>
  
  <pre><code>SELECT slug, name, count(client_tests.id) AS test_count
    FROM clients
    LEFT JOIN client_tests ON clients.id = client_tests.client_id
    GROUP BY clients.id;
  </code></pre>
  
  <p>I've tried to build a join query, but Lithium seems to be expecting the join to be part of a relationship (no relationships are defined), as shown below (ignoring the count for now).</p>
  
  <pre><code>$join = new Query(array(
    'source' =&gt; 'client_tests',
    'model' =&gt; '\app\models\ClientTest',
    'type' =&gt; 'LEFT',
    'constraint' =&gt; array('Client.id' =&gt; 'ClientTest.client_id'),
  ));
  $clients = Client::all(array(
    'conditions' =&gt; $conditions,
    'group' =&gt; 'Client.id',
    'joins' =&gt; array($join)
  ));
  </code></pre>
  
  <p>This results in <code>Notice: Undefined index: ClientTest in /usr/local/www/oars/libraries/lithium/data/collection/RecordSet.php on line 340</code>, which seems to be relationship-related code.</p>
  
  <p>If I do define a <code>hasMany</code> relationship between Client and ClientTest, which will handle building the left join for me, is there a way to get the Client fields and the count?</p>
  
  <pre><code>$clients = Client::all(array(
    'fields' =&gt; array('slug', 'name', 'count(ClientTest.test_id) as test_count'),
    'conditions' =&gt; $conditions,
    'group' =&gt; 'Client.id',
    'with' =&gt; 'ClientTest'
  ));
  </code></pre>
  
  <p>This results in <code>( ! ) Notice: Undefined index: count(ClientTest in /usr/local/www/oars/libraries/lithium/data/source/Database.php on line 650</code>, so either it's not possible or I'm using the wrong syntax.</p>
  
  <p>I could simply issue the query directly with <code>Client::connection()-&gt;read($sql)</code>, but as there are dynamic elements to the query, I'd have to build the SQL anyway.</p>
  
  <p>Is there a way to get the above methods to work, or should I go with manually building the SQL?</p>