Doctrine - how to get the SQL INSERT query, in the postSave() event?

后端 未结 5 945
一向
一向 2021-02-09 05:57

I would like to get the exact SQL INSERT query that Doctrine generates when an object\'s save() method is called.

Preferably, I would like to get it in the postSave() ev

5条回答
  •  南旧
    南旧 (楼主)
    2021-02-09 06:22

    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).

    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();
    

    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().

    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.

提交回复
热议问题