使用 createCommand 打印 sql 报错 [ 2.0 版本 ]
$userinfo = new UserInfo;
$userinfo->user_name = $data['user_name'];
$userinfo->telephone = $data['telephone'];
$userinfo->head_img = $data['head_img'];
$userinfo->type = $data['type'];
$userinfo->base_type = $data['base_type'];
$userinfo->createon = time();
$query = clone $userinfo;
$sql = $query->createCommand()->getSql();
var_dump($sql);die();
{
"name": "Exception",
"message": "Calling unknown method: api\\models\\UserInfo::createCommand()",
"code": 0,
"type": "yii\\base\\UnknownMethodException",
"file": "E:\\WWW\\test.test\\vendor\\yiisoft\\yii2\\base\\Component.php",
"line": 300,
"stack-trace": [
"#0 E:\\WWW\\test.test\\api\\models\\UserInfo.php(93): yii\\base\\Component->__call('createCommand', Array)",
"#1 E:\\WWW\\test.test\\api\\controllers\\v1\\UserController.php(59): api\\models\\UserInfo->addUserInfo(Array)",
"#2 [internal function]: api\\controllers\\v1\\UserController->actionLogin()",
"#3 E:\\WWW\\test.test\\vendor\\yiisoft\\yii2\\base\\InlineAction.php(57): call_user_func_array(Array, Array)",
"#4 E:\\WWW\\test.test\\vendor\\yiisoft\\yii2\\base\\Controller.php(157): yii\\base\\InlineAction->runWithParams(Array)",
"#5 E:\\WWW\\test.test\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('login', Array)",
"#6 E:\\WWW\\test.test\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('v1/user/login', Array)",
"#7 E:\\WWW\\test.test\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
"#8 E:\\WWW\\test.test\\api\\web\\index.php(17): yii\\base\\Application->run()",
"#9 {main}"
]
}
vendor\\yiisoft\\yii2\\base\\Component.php
这个文件里面有 createCommand
方法啊
共 2 个回答
-
createCommand 是 yii\db\ActiveQuery 类的方法。只有在 find 操作的时候才能使用。
/** * {@inheritdoc} * @return ActiveQuery the newly created [[ActiveQuery]] instance. */ public static function find() { return Yii::createObject(ActiveQuery::className(), [get_called_class()]); }
共 1 条回复Evanwang03 回复于 2018-08-09 09:34 回复update也可以吧,记得之前用过啊,要不update没法看sql?
Evanwang03
注册时间:2015-08-27
最后登录:2019-08-16
在线时长:10小时12分
最后登录:2019-08-16
在线时长:10小时12分
- 粉丝2
- 金钱110
- 威望0
- 积分210