title: How to use CDbCriteria in Yii to apply SQL filters on Model
body: <p>I try to get this mysql query to work with Yii model but i can't. </p>
  
  <pre><code>SELECT COUNT( qhc.countries_id) AS counter, q.question, co.name 
  FROM questions AS q , countries as co, questions_has_countries AS qhc
  WHERE qhc.questions_id = q.id 
  AND co.id = qhc.countries_id
  GROUP BY question
  HAVING counter = 2
  </code></pre>
  
  <p>So far i have this, but somehow thou it seems ok, it doesnt work : </p>
  
  <pre><code>$criteria = new CDbCriteria(); 
  $criteria-&gt;select = 'question, COUNT(countries_id) as counter';
  $criteria-&gt;with = array('countries', 'categories');
  $criteria-&gt;addInCondition('countries.id' , $_POST['Questions']['countries']);
  $criteria-&gt;group = 'question';
  $criteria-&gt;having = ('counter = 1');
  $model = Questions::model()-&gt;findAll($criteria)
  </code></pre>
  
  <p>Pls help, I'am pretty new to Yii framework. </p>
  
  <p>Thanks. </p>
  
  <p>Sql from the log : </p>
  
  <pre><code>SELECT `t`.`question` AS `t0_c1`,
  COUNT(countries_id) as counter, `t`.`id` AS `t0_c0`, `countries`.`id` AS
  `t1_c0`, `countries`.`name` AS `t1_c1`, `categories`.`id` AS `t2_c0`,
  `categories`.`name` AS `t2_c1` FROM `questions` `t`  LEFT OUTER JOIN
  `questions_has_countries` `countries_countries` ON
  (`t`.`id`=`countries_countries`.`questions_id`) LEFT OUTER JOIN `countries`
  `countries` ON (`countries`.`id`=`countries_countries`.`countries_id`) 
  LEFT OUTER JOIN `questions_has_categories` `categories_categories` ON
  (`t`.`id`=`categories_categories`.`questions_id`) LEFT OUTER JOIN
  `categories` `categories` ON
  (`categories`.`id`=`categories_categories`.`categories_id`)  WHERE
  (countries.id=:ycp0) GROUP BY question HAVING (counter = 2). Bound with
  :ycp0='1'
  </code></pre>