Execute raw SQL using Doctrine 2

Jiew Meng picture Jiew Meng · Jul 24, 2010 · Viewed 176.2k times · Source

I want to execute raw SQL using Doctrine 2

I need to truncate the database tables and initialize tables with default test data.

Answer

Jason Swett picture Jason Swett · Mar 30, 2012

Here's an example of a raw query in Doctrine 2 that I'm doing:

public function getAuthoritativeSportsRecords()
{   
    $sql = " 
        SELECT name,
               event_type,
               sport_type,
               level
          FROM vnn_sport
    ";

    $em = $this->getDoctrine()->getManager();
    $stmt = $em->getConnection()->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll();
}