Using Raw SQL with Doctrine

后端 未结 6 2002
礼貌的吻别
礼貌的吻别 2020-12-02 10:39

I have some extremely complex queries that I need to use to generate a report in my application. I\'m using symfony as my framework and doctrine as my ORM.

My quest

6条回答
  •  感动是毒
    2020-12-02 11:19

    I'm not sure what do you mean saying raw SQL, but you coud execute traditional SQL queries this way:

    ... 
    // $this->_displayPortabilityWarning();
    
    $conn = Doctrine_Manager::connection();
    $pdo = $conn->execute($sql);
    $pdo->setFetchMode(Doctrine_Core::FETCH_ASSOC);
    $result = $pdo->fetchAll();
    ...
    

    The following method is not necsessary, but it shows a good practice.

    protected function _displayPortabilityWarning($engine = 'pgsql')
    {
         $conn = Doctrine_Manager::connection();
         $driver = $conn->getDriverName();
    
         if (strtolower($engine) != strtolower($driver)) {
            trigger_error('Here we have possible database portability issue. This code was tested on ' . $engine . ' but you are trying to run it on ' . $driver, E_USER_NOTICE);
         }
    }
    

提交回复
热议问题