Sub-queries ActiveRecord Yii

后端 未结 3 733
天涯浪人
天涯浪人 2020-12-09 21:31

Is it possible to make sub-queries in ActiveRecord in Yii?

i have a query like this:

select * from table1 where table1.field1 in (select table2.field2

相关标签:
3条回答
  • 2020-12-09 21:59

    I know this an old thread but maybe someone (like me) still needs an answer.

    There is a small issues related to the previous answers. So, here is my enhancement:

    $model=new SomeModel();
    $criteria=new CDbCriteria();
    $criteria->compare('attribute', $value);
    $criteria->addCondition($condition);
    // ... etc
    $subQuery=$model->getCommandBuilder()->createFindCommand($model->getTableSchema(),$criteria)->getText();
    
    $mainCriteria=new CDbCriteria();
    $mainCriteria->addCondition($anotherCondition);
    // ... etc
    
    // NOW THIS IS IMPORTANT 
    $mainCriteria->params = array_merge($criteria->params, $mainCriteria->params);
    
    // Now You can pass the criteria:
    $result = OtherModel::model()->findAll($mainCriteria);
    
    0 讨论(0)
  • 2020-12-09 22:10

    First find doublets 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,
    ));
    

    Source: http://www.yiiframework.com/wiki/364/using-sub-query-for-doubletts/

    0 讨论(0)
  • 2020-12-09 22:11

    No, there is not a way to programmatically construct a subquery using Yii's CDbCriteria and CActiveRecord. It doesn't look like the Query Builder has a way, either.

    You can still do subqueries a few different ways, however:

    $results = Object1::model()->findAll(array(
      'condition'=>'t.field1 in (select table2.field2 from table2)')
    );
    

    You can also do a join (which will probably be faster, sub-queries can be slow):

    $results = Object1::model()->findAll(array(
      'join'=>'JOIN table2 ON t.field1 = table2.field2'
    );
    

    You can also do a direct SQL query with findAllBySql:

    $results = Object1::model()->findAllBySql('
      select * from table1 where table1.field1 in 
      (select table2.field2 from table2)'
    );
    

    You can, however, at least provide a nice AR style interface to these like so:

    class MyModel extends CActiveRecord {
      public function getResults() {
        return Object1::model()->findAll(array(
          'condition'=>'t.field1 in (select table2.field2 from table2)')
        );
      }
    }
    

    Called like so:

    $model = new MyModel();
    $results = $model->results;
    

    One interesting alternative idea would be to create your subquery using the Query Builder's CDbCommand or something, and then just pass the resulting SQL query string into a CDbCritera addInCondition()? Not sure if this will work, but it might:

    $sql = Yii::app()->db->createCommand()
      ->select('*')
      ->from('tbl_user')
      ->text;
    $criteria->addInCondition('columnName',$sql);
    

    You can always extend the base CDbCriteria class to process and build subqueries somehow as well. Might make a nice extension you could release! :)

    I hope that helps!

    0 讨论(0)
提交回复
热议问题