Using Raw SQL with Doctrine

后端 未结 6 2005
礼貌的吻别
礼貌的吻别 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:04

    Yes. You can get a database handle from Doctrine using the following code:

    $pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
    

    and then execute your SQL as follows:

    $query = "SELECT * FROM table WHERE param1 = :param1 AND param2 = :param2";
    $stmt = $pdo->prepare($query);
    
    $params = array(
      "param1"  => "value1",
      "param2"  => "value2"
    );
    $stmt->execute($params);
    
    $results = $stmt->fetchAll();  
    

    You can use bound variables as in the above example.

    Note that Doctrine won't automatically hydrate your results nicely into record objects etc, so you'll need to deal with the results being returned as an array, consisting of one array per row returned (key-value as column-value).

提交回复
热议问题