title: Cake HABTM Query, Order By Rand()
body: <p>I'm aware that Cake HABTM associations are tricky at the best of times, but I seem to be making life even harder for myself...</p>
  
  <p>If I want to return a random Item from the db, I can do it as follows in the Item model:</p>
  
  <pre><code>$random = $this-&gt;find('first', array(
      'order' =&gt; 'rand()'
  ));
  </code></pre>
  
  <p>and if I want to find all the Items that are in a certain Category (where Item has a HABTM relationship to Categories), I know I can get a result set through $this->Categories->find.</p>
  
  <p>My question is: how can I combine the two, so I can return a random Item that belongs to a specified Category?  Is there any easy way?  (If not, I'll gladly take any suggestions for a laborious way, as long as it works ;)</p>
  
  <p>ETA: I can get some of the way with Containable, maybe: say I add the line</p>
  
  <pre><code>'contain' =&gt; array('Categories'=&gt;array('conditions'=&gt;array('Categories.id'=&gt;1))),
  </code></pre>
  
  <p>then the Item results that I don't want come back with an empty Categories array, to distinguish them from "good" items.  But really I don't want said Item results to be returned at all...</p>
  
  <p>ETA(2): I can get a workaround going by unsetting my results in the afterFind if the Categories array is empty (thanks to <a href="http://nuts-and-bolts-of-cakephp.com/2008/08/06/filtering-results-returned-by-containable-behavior/" rel="noreferrer">http://nuts-and-bolts-of-cakephp.com/2008/08/06/filtering-results-returned-by-containable-behavior/</a> for the tip), and then having my random find function not give up until it gets a result:</p>
  
  <pre><code>while (!is_array($item)) {
      $item = $this-&gt;random($cat);
  }
  </code></pre>
  
  <p>but ugh, could this be any clunkier?  Anyway, time for me to stop editing my question, and to go away and sleep on it instead!</p>