Doctrine - How to print out the real sql, not just the prepared statement?

后端 未结 19 2387
自闭症患者
自闭症患者 2020-11-30 18:59

We\'re using Doctrine, a PHP ORM. I am creating a query like this:

$q = Doctrine_Query::create()->select(\'id\')->from(\'MyTable\');

19条回答
  •  一整个雨季
    2020-11-30 19:44

    Modified @dsamblas function to work when parameters are date strings like this '2019-01-01' and when there is array passed using IN like

    $qb->expr()->in('ps.code', ':activeCodes'),
    

    . So do everything what dsamblas wrote, but replace startQuery with this one or see the differences and add my code. (in case he modified something in his function and my version does not have modifications).

    public function startQuery($sql, array $params = null, array $types = null)
    
    {
        if($this->isLoggable($sql)){
            if(!empty($params)){
                foreach ($params as $key=>$param) {
    
                    try {
                        $type=Type::getType($types[$key]);
                        $value=$type->convertToDatabaseValue($param,$this->dbPlatform);
                    } catch (Exception $e) {
                        if (is_array($param)) {
                            // connect arrays like ("A", "R", "C") for SQL IN
                            $value = '"' . implode('","', $param) . '"';
                        } else {
                            $value = $param; // case when there are date strings
                        }
                    }
    
                    $sql = join(var_export($value, true), explode('?', $sql, 2));
                }
    
            }
            echo $sql . " ;".PHP_EOL;
        }
    }
    

    Did not test much.

提交回复
热议问题