$db = \Yii::$app->getDb();
$select = [
'c1'=>new \yii\db\Expression('(select count(*) from eleFenceCenter.elefence_menu)'),
'c2'=>new \yii\db\Expression('(select count(*) from eleFenceCenter.elefence_auth_item)')
];
$q = new \yii\db\Query();
$rs = $q->select($select)->one();
$policeQuery = ServiceInfo::find()->alias('service')->where(["`service`.`area_code`"=>$areaArr])
->select([
'`service`.`area_code`',
'police_code'=>"IF(service.police_code<>'',service.police_code,service.service_code)",
"police_name"=> "IF(`service`.`police_code`<>'',`police`.`police_name`,`service`.`service_name`)",
"type"=>"IF(`service`.`police_code`<>'','Police','Service')"])
->joinWith([
'police' => function ($q){
return $q->alias('police')->select(['police_code','police_name']);
}
])->distinct(TRUE)->orderBy("`service`.`police_code`")->all();
最后得到的结果数组中,肯定含有police 的键值 我当时是想怎么查询想要的键 比如不要police键。
@xjdata ,
namespace app\components\db;
use yii\db\Command;
class Connection extends \yii\db\Connection
{
public static $instance = null;
public function createCommand($sql = null,$params = [])
{
if (!self::$instance)
{
$command = new $this->commandClass([
'db' => $this,
'sql' => $sql,
]);
}
else
{
$command = self::$instance;
$command->setSql($sql);
}
return self::$instance = $command->bindValues($params);
}
public function getLastSql()
{
$instance = self::$instance;
return $instance->getRawSql();
}
}
然后更改db.php配置项`
php 'class' => 'app\components\db\Connection' `
然后在具体的SQL操作后,使用\Yii::$app->getDb()->getLastSql()
,貌似就可以得到最后操作的SQL了。
有没有其他的影响,就不清楚了哦 :smile:
@forecho ,ajax 请求跳转,JS客户端要有这部分代码:(yii.js应该有类似的代码功能)
$(document).ajaxComplete(function (event, xhr, settings) {
var url = xhr.getResponseHeader('X-Redirect');
if (url) {
window.location = url;
}
});