doctrine - 如何在postSave()事件中获取SQL INSERT查询? [英] Doctrine - how to get the SQL INSERT query, in the postSave() event?
问题描述
我想在调用对象的save()方法时得到Doctrine生成的确切的SQL INSERT查询。
I would like to get the exact SQL INSERT query that Doctrine generates when an object's save() method is called.
最好我想得到模型的postSave()事件并将其记录在txt文件中。
Preferably, I would like to get it in the postSave() event of the model and log it in a txt file.
例如:
<?php
$user = new User(); // A Doctrine Model with timestampable behavior enabled
$user->first_name = 'Manny';
$user->last_name = 'Calavera';
$user->save();
?>
我想获取/记录以下SQL查询:
I want to get/log the following SQL query:
INSERT INTO user (first_name, last_name, created_at, updated_at) VALUES ('Manny', 'Calavera', '2010-08-03 12:00:00', '2010-08-03 12:00:00');
需要这个的背景是,我希望通过解析txt来稍后大量导入数据文件。
The background for needing this, is that I wish to mass-import later the data by parsing the txt file.
推荐答案
我不认为有任何简单的方法可以这样做,因为save()的行为会因为几个不同的东西(如果你正在插入/更新)。
I don't think there is any easy way to do this since the behaviour of save() varies depending on a few different things (if you're inserting/updating).
如果你创建了一个教义查询对象,那么你可以像这样调用getSqlQuery()方法:
If you had created a doctrine query object, then you can call the getSqlQuery() method like this:
$q = Doctrine_Query::create()
->select('u.id')
->from('User u');
echo $q->getSqlQuery();
但是save()是一个方法,而不是一个对象,所以这将不起作用。我想你必须在一些代码中检测是否插入或更新并构建一个查询来登录,然后使用save()。
but the save() is a method, not an object so this won't work. I think you'll have to hack in some code to detect whether you're inserting or updating and build a query to log on the fly and then use save().
我知道这个建议不是很理想,因为它没有记录'save()正在做什么,但是为了你所说的目的,它仍然可以正常工作。
I know this suggestion is not ideal because it is not logging 'exactly' what save() is doing but for the purposes you stated it should still work just as well.
这篇关于doctrine - 如何在postSave()事件中获取SQL INSERT查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!