Yii - How to print SQL used by findAll

前端 未结 5 1621
庸人自扰
庸人自扰 2021-01-04 04:16

I have the following code to get some records from db

    $criteria = new CDbCriteria();
    $criteria->condition = \'t.date BETWEEN \"\'.$from_date.\'\"          


        
5条回答
  •  执念已碎
    2021-01-04 04:21

    If you don't want to execute the query before seeing the SQL, this isn't actually as easy as you might hope.

    It's as dirty as wrong but, when in development only, I have in the past taken to adding a deliberate a deliberate error in the criteria and relying on the resulting Exception to give the SQL attempted.

    e.g.

    $criteria = new CDbCriteria();
    $criteria->condition = 't.date_fgjhfgjfgj BETWEEN :from_date AND :to_date';
    $criteria->params = array(
      ':from_date' => $from_date,
      ':to_date' => $to_date,
    );
    $criteria->with = array('order');
    $orders = ProductOrder::model()->findAll($criteria);
    

    I have found Ilya's method to be unreliable (don't know why, but sometimes the criteria is ignored using this method).

提交回复
热议问题