title: CakePHP query additions in controller
body: <p>I am migrating raw PHP code to CakePHP and have some problems. As I have big problems with query to ORM transformation I temporary use raw SQL. All is going nice, but I met the ugly code and don't really know how to make it beautiful. I made <code>DealersController</code> and added <code>function advanced($condition = null)</code> (it will be called from AJAX with parameters 1-15 and 69). function looks like:</p>
  
  <pre><code>switch ($condition) {
    case '1':
      $cond_query = ' AND ( (d.email = \'\' OR d.email IS NULL) )';
    break;
    case '2':
      $cond_query = ' AND (d.id IN (SELECT dealer_id FROM dealer_logo)';
    break;
    // There are many cases, some long, some like these two
  }
  
  if($user_group == 'group_1') {
    $query = 'LONG QUERY WITH 6+ TABLES JOINING' . $cond_query;
  } elseif ($user_group == 'group_2'){
    $query = 'A LITLE BIT DIFFERENT LONG QUERY WITH 6+ TABLES JOINING' . $cond_query;
  } else {
    $query = 'A LITLE MORE BIT DIFFERENT LONG QUERY WITH 10+ TABLES JOINING' . $cond_query;
  } 
  
  // THERE IS $this-&gt;Dealer-&gt;query($query); and so on
  </code></pre>
  
  <p>So.. As you see code looks ugly. I have two variants:  </p>
  
  <p>1) get out query addition and make model methods for every condition, then these conditions seperate to functions. But this is not DRY, because main 3 big queries is almost the same and if I will need to change something in one - I will need to change 16+ queries.  </p>
  
  <p>2) Make small reusable model methods/queries whitch will get out of DB small pieces of data, then don't use raw SQL but play with methods. It would be good, but the performance will be low and I need it as high as possible.</p>
  
  <p>Please give me advice. Thank you!</p>