Yii2:获取模型的原始sql->保存()
Yii2: get raw sql of model->save()
我想用 ActiveRecord
将记录添加到 table。
这是我的代码:
$model = new Article();
$model->title = 'test title';
$model->content = 'test content';
$model->category_id = 1;
$model->author_id = 1;
if ($model->validate()) {
$model->save();
}
$model->validate()
returnstrue
,但是$model->save()
returnsfalse
.
如何找到 $model->save()
的生成的原始 sql?
同时:
$model->save()->rawSql
是 null
和 $model->getErrors()
returns 空数组。
在调试中,记录了所有查询,但我没有找到任何插入或更新查询。
$model->save()->rawSql
调用不能returnnull
,它必须抛出一个异常,说明你正试图访问属性的非对象。 $model->save()
returns boolean
值 - 查询是否成功执行。
如果 $model->getErrors()
return 的空数组和查询根本没有执行,我很确定模型事件处理程序有问题,尤其是 beforeSave()
,检查一下,它不应该 return false
。还要检查附加的行为事件处理程序。
至于查询。如果它只是没有被执行它是没有用的,但是如果它被执行了,这里有一些方法可以实现它:
1) 可能是最好的方法。使用调试面板。我也提到了here.
2) 按照@robsch 的建议查看日志。
您不能直接在代码中使用 $model->save()
获取原始 SQL,它将调用 insert()
或 update()
。如果您有兴趣,这里是 insertInternal()
:
的部分代码
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
foreach ($this->getPrimaryKey(true) as $key => $value) {
$values[$key] = $value;
}
}
$db = static::getDb();
$command = $db->createCommand()->insert($this->tableName(), $values);
if (!$command->execute()) {
return false;
}
如果你调用 $command->rawSql
你会得到原始的 sql 但你不能在外面这样做,因为命令是在内部形成的。
P.S.这段代码:
if ($model->validate()) {
$model->save();
}
没有意义,因为 $model->save()
会在内部调用 $model->validate()
。
此代码不会向您显示原始 sql 但您将获得查询预绑定和参数
try {
// this will simulate $model->save();
$builder = $model->getCommandBuilder();
$table = $model->getTableSchema();
$command = $builder->createInsertCommand($table, $model->getAttributes());
$command->_connection->enableParamLogging = true;
$command->execute();
} catch (Exception $e) {
// getText() will print the query with the binding params
// getAttributes() will give you the attributes injected
var_dump($command->getText());
var_dump($model->getAttributes());
die();
}
结果将如下所示:
"INSERT INTO `fruit` (`order`, `name`, `season`) VALUES (:yp0, :yp1,:yp2)"
array(2) {
["order"] => int(1),
["name"] => null,
["season"] => null
}
我想用 ActiveRecord
将记录添加到 table。
这是我的代码:
$model = new Article();
$model->title = 'test title';
$model->content = 'test content';
$model->category_id = 1;
$model->author_id = 1;
if ($model->validate()) {
$model->save();
}
$model->validate()
returnstrue
,但是$model->save()
returnsfalse
.
如何找到 $model->save()
的生成的原始 sql?
同时:
$model->save()->rawSql
是 null
和 $model->getErrors()
returns 空数组。
在调试中,记录了所有查询,但我没有找到任何插入或更新查询。
$model->save()->rawSql
调用不能returnnull
,它必须抛出一个异常,说明你正试图访问属性的非对象。 $model->save()
returns boolean
值 - 查询是否成功执行。
如果 $model->getErrors()
return 的空数组和查询根本没有执行,我很确定模型事件处理程序有问题,尤其是 beforeSave()
,检查一下,它不应该 return false
。还要检查附加的行为事件处理程序。
至于查询。如果它只是没有被执行它是没有用的,但是如果它被执行了,这里有一些方法可以实现它:
1) 可能是最好的方法。使用调试面板。我也提到了here.
2) 按照@robsch 的建议查看日志。
您不能直接在代码中使用 $model->save()
获取原始 SQL,它将调用 insert()
或 update()
。如果您有兴趣,这里是 insertInternal()
:
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
foreach ($this->getPrimaryKey(true) as $key => $value) {
$values[$key] = $value;
}
}
$db = static::getDb();
$command = $db->createCommand()->insert($this->tableName(), $values);
if (!$command->execute()) {
return false;
}
如果你调用 $command->rawSql
你会得到原始的 sql 但你不能在外面这样做,因为命令是在内部形成的。
P.S.这段代码:
if ($model->validate()) {
$model->save();
}
没有意义,因为 $model->save()
会在内部调用 $model->validate()
。
此代码不会向您显示原始 sql 但您将获得查询预绑定和参数
try {
// this will simulate $model->save();
$builder = $model->getCommandBuilder();
$table = $model->getTableSchema();
$command = $builder->createInsertCommand($table, $model->getAttributes());
$command->_connection->enableParamLogging = true;
$command->execute();
} catch (Exception $e) {
// getText() will print the query with the binding params
// getAttributes() will give you the attributes injected
var_dump($command->getText());
var_dump($model->getAttributes());
die();
}
结果将如下所示:
"INSERT INTO `fruit` (`order`, `name`, `season`) VALUES (:yp0, :yp1,:yp2)"
array(2) {
["order"] => int(1),
["name"] => null,
["season"] => null
}