yii2 部分很实用的代码

检查执行的sql

$query->createCommand()->getRawSql()
 
 
写日志

// 记录日志

\Yii::error('生成失败.' . $index, $this->id);
 
Yii::trace():记录一条消息去跟踪一段代码是怎样运行的。这主要在开发的时候使用。
Yii::info():记录一条消息来传达一些有用的信息。
Yii::warning():记录一个警告消息用来指示一些已经发生的意外。
Yii::error():记录一个致命的错误,这个错误应该尽快被检查。
 
批量插入
$res = \Yii::$app->db->createCommand()->batchInsert(
test::tableName(), [
'id',
'name',
], $param
)->execute()
;
 
yii2 exists 用法
不能实现 提示 Calling unknown method: yii\\db\\Query::getQueryBuilder()
$query->exists((new Query())->select('c.submitId')->from(BUrgeToReport::tableName() . ' as c ')->where(['c.submitId' => 'a.id']));
成功实现
$query->where([
'exists',
(new Query())->from(BUrgeToReport::tableName() . ' as c ')->where(['c.submitId' => 'a.id'])->select('c.submitId')
]);
参考: https://www.cnblogs.com/sign-ptk/p/7873613.html
A_AR::find()
->where([
'exists',
B_AR::find()->where("interval_id={{A}}.id")->andWhere(['code' => 'a'])
]);
红色部分需要这样写,如果写成
->where(['interval_id' => '{{A}}.id'])
会把条件'{{A}}.id'按字符串查询!

$query->where(

[
'exists',
// BUrgeToReport::find()->alias('c')->where(['c.submitId' => 'a.id'])->select('*')
// (new Query())->from(BUrgeToReport::tableName() . ' as c ')->where(['c.submitId' => 'a.id'])->select('c.id')
(new Query())->from(BUrgeToReport::tableName() . ' as c ')->where('c.submitId = a.id')->select('c.id')
]
);
 

yii2框架 模拟发起http请求,修改头部,查看返回头部数据

yii2框架 模拟发起http请求-httpclient客户端 

use yii\httpclient\Client;

$client = new Client(['baseUrl' => 'http://example.com/api/1.0']);
$request = $client->createRequest()
->setHeaders(['content-type' => 'application/json'])
->addHeaders(['user-agent' => 'My User Agent']);

$request->getHeaders()->add('accept-language', 'en-US;en');
$request->headers->set('user-agent', 'User agent override');
在响应对象中可以获取header信息
$response = $request->send();
echo $response->getHeaders()->get('content-type');
echo $response->headers->get('content-encoding');
---------------------
作者:bingcool空间
来源:CSDN
原文:https://blog.csdn.net/u012979009/article/details/52290584
版权声明:本文为博主原创文章,转载请附上博文链接! 

转载于:https://www.cnblogs.com/stillstep/p/10181070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值