Using sub query for doubletts

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

at first find doubletts by db fields:

$model=new MyModel('search');
$model->unsetAttributes();
        
$criteria=new CDbCriteria();
$criteria->select='col1,col2,col3';
$criteria->group = 'col1,col2,col3';
$criteria->having = 'COUNT(col1) > 1 AND COUNT(col2) > 1 AND COUNT(col3) > 1';

get the subquery:

$subQuery=$model->getCommandBuilder()->createFindCommand($model->getTableSchema(),$criteria)->getText();

add the subquery condition:

$mainCriteria=new CDbCriteria();
$mainCriteria->condition=' (col1,col2,col3) in ('.$subQuery.') ';
$mainCriteria->order = 'col1,col2,col3';

how to use:

$result = MyModel::model()->findAll($mainCriteria);

or
$dataProvider = new CActiveDataProvider('MyModel', array(
        'criteria'=>$mainCriteria,
));