119

I want to execute raw SQL using Doctrine 2

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

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • 2
    By the way, when I want to do automated database gruntwork, like doing `mysqldump`s or loading in data from previous dumps or dropping tables, I usually write a shell script for that work and then write a task (or "command", in Symfony2 language) that executes the shell script. The purpose of an ORM, as I understand it, is to abstract away repetitive work, and if you're doing something like truncating a table, I don't see how it would make sense to bring Doctrine into the picture since Doctrine doesn't make that task any easier. – Jason Swett Mar 30 '12 at 15:18

9 Answers9

183

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();
}   
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
Jason Swett
  • 43,526
  • 67
  • 220
  • 351
  • 4
    Nice answer. To get entity manager in this code, you may use **$this->getDoctrine()->getManager()** in place of this code above **"$this->getEntityManager()"**, this way it worked for me straight away. – webblover Aug 22 '14 at 15:59
  • hey its giving me Call to undefined method Index::getDoctrine() what should i do – Dexter Dec 19 '14 at 08:02
  • i m using codeigniter with doctrine 2 http://wildlyinaccurate.com/integrating-doctrine-2-with-codeigniter-2 – Dexter Dec 19 '14 at 08:05
  • 2
    This led me in the right direction but wasn't exactly what I needed. I suspect that the age of the answer makes a difference. I used: `...getConnection()->query($sql);` and didn't have to run `$stmt->execute();` – Brandon Apr 04 '17 at 18:54
  • Note that with Symfony4 and autowiring, you can can type hint `EntityManagerInterface $entityManager` and then call `$entityManager->getConnection()` – COil Jul 17 '19 at 12:26
  • **DEPRECATED**: `execute` and `fetchAll` are deprecated in **Doctrine DBAL >2.13**, see [my answer for more information](https://stackoverflow.com/a/69885184/11350193). – Dylan KAS Nov 08 '21 at 14:36
59
//$sql - sql statement
//$em - entity manager

$em->getConnection()->exec( $sql );
orourkedd
  • 6,201
  • 5
  • 43
  • 66
54

I got it to work by doing this, assuming you are using PDO.

//Place query here, let's say you want all the users that have blue as their favorite color
$sql = "SELECT name FROM user WHERE favorite_color = :color";

//set parameters 
//you may set as many parameters as you have on your query
$params['color'] = blue;


//create the prepared statement, by getting the doctrine connection
$stmt = $this->entityManager->getConnection()->prepare($sql);
$stmt->execute($params);
//I used FETCH_COLUMN because I only needed one Column.
return $stmt->fetchAll(PDO::FETCH_COLUMN);

You can change the FETCH_TYPE to suit your needs.

Fernando
  • 750
  • 7
  • 21
  • 1
    Best example out of all of them – David Aug 22 '18 at 07:21
  • 1
    **DEPRECATED**: `execute` and `fetchAll` are deprecated in **Doctrine DBAL >2.13**, see [my answer for more information](https://stackoverflow.com/a/69885184/11350193). – Dylan KAS Nov 08 '21 at 14:38
18

Most of the answers here are now deprecated since Doctrine DBAL 2.13. For example, execute is deprecated and fetchAll will be removed in 2022.

/**
 * BC layer for a wide-spread use-case of old DBAL APIs
 *
 * @deprecated This API is deprecated and will be removed after 2022
 *
 * @return list<mixed>
 */
public function fetchAll(int $mode = FetchMode::ASSOCIATIVE): array

It's no longer recommended to use execute and then fetchAll since both are deprecated.

* @deprecated Statement::execute() is deprecated, use Statement::executeQuery() or executeStatement() instead

* @deprecated Result::fetchAll is deprecated, and will be removed after 2022

So we have to be more specific when executing raw SQL as well as fetching result.


Instead of using Statement::execute(), we need to use executeQuery or executeStatement.

executeQuery return object Result:

Executes the statement with the currently bound parameters and return result.

executeStatement return int:

Executes the statement with the currently bound parameters and return affected rows.


Instead of using Result::fetchAll(), we need to use fetchAllNumeric or fetchAllAssociative (and more).


To get a simple result, you would have to do:

public function getSqlResult(EntityManagerInterface $em)
{   
    $sql = " 
        SELECT firstName,
               lastName
          FROM app_user
    ";

    $stmt = $em->getConnection()->prepare($sql);
    $result = $stmt->executeQuery()->fetchAllAssociative();
    return $result;
}   

And with parameters:

public function getSqlResult(EntityManagerInterface $em)
{   
    $sql = " 
        SELECT firstName,
               lastName,
               age
          FROM app_user
          where age >= :age
    ";

    $stmt = $em->getConnection()->prepare($sql);
    $stmt->bindParam('age', 18);
    $result = $stmt->executeQuery()->fetchAllAssociative();
    return $result;
}   
Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
14

How to execute a raw Query and return the data.

Hook onto your manager and make a new connection:

$manager = $this->getDoctrine()->getManager();
$conn = $manager->getConnection();

Create your query and fetchAll:

$result= $conn->query('select foobar from mytable')->fetchAll();

Get the data out of result like this:

$this->appendStringToFile("first row foobar is: " . $result[0]['foobar']);
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • 1
    query() is for when the SQL returns some data you want to use; exec() is for when it doesn't – Jeffiekins Nov 12 '18 at 21:07
  • **DEPRECATED**: `exec`, `execute` and `fetchAll` are deprecated in **Doctrine DBAL >2.13**, [see my answer for more information.](https://stackoverflow.com/a/69885184/11350193) – Dylan KAS Nov 25 '21 at 10:44
12

I found out the answer is probably:

A NativeQuery lets you execute native SQL, mapping the results according to your specifications. Such a specification that describes how an SQL result set is mapped to a Doctrine result is represented by a ResultSetMapping.

Source: Native SQL.

Community
  • 1
  • 1
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • 18
    This is the accepted answer but I still don't see how this part of Doctrine is useful because you always need the ResultSetMapping. I don't want it to map the results to Entities .... that defaults the point of running arbitrary SQL! – MikeMurko Nov 02 '11 at 00:36
  • 2
    @MikeMurko I found this post helpful for running raw queries in Doctrine 2: http://forum.symfony-project.org/viewtopic.php?f=23&t=37872 – Jason Swett Mar 30 '12 at 15:11
  • Also **Non-native** Native SQL will not execute every possible SQL query. DELETE/UPDATE/INSERT wont work, nor some table definitions that do not follow doctrine assumptions. (M2M joining table without ids). So this answer is not universal. Nor should be accepted as INSERTs wont work. – przemo_li Nov 24 '15 at 12:08
  • The accepted answer has an invalid link now. – Mahmoud Abdelsattar Sep 09 '22 at 11:36
5

In your model create the raw SQL statement (example below is an example of a date interval I had to use but substitute your own. If you are doing a SELECT add ->fetchall() to the execute() call.

   $sql = "DELETE FROM tmp 
            WHERE lastedit + INTERVAL '5 minute' < NOW() ";

    $stmt = $this->getServiceLocator()
                 ->get('Doctrine\ORM\EntityManager')
                 ->getConnection()
                 ->prepare($sql);

    $stmt->execute();
badzilla
  • 75
  • 1
  • 3
5

I had the same problem. You want to look the connection object supplied by the entity manager:

$conn = $em->getConnection();

You can then query/execute directly against it:

$statement = $conn->query('select foo from bar');
$num_rows_effected = $conn->exec('update bar set foo=1');

See the docs for the connection object at http://www.doctrine-project.org/api/dbal/2.0/doctrine/dbal/connection.html

Toby Batch
  • 169
  • 1
  • 2
4

You can't, Doctrine 2 doesn't allow for raw queries. It may seem like you can but if you try something like this:

$sql = "SELECT DATE_FORMAT(whatever.createdAt, '%Y-%m-%d') FORM whatever...";
$em = $this->getDoctrine()->getManager();
$em->getConnection()->exec($sql);

Doctrine will spit an error saying that DATE_FORMAT is an unknown function.

But my database (MySQL) does know that function, so basically what is happening is Doctrine is parsing that query behind the scenes (and behind your back) and finding an expression that it doesn't understand, considering the query to be invalid.

So if like me you want to be able to simply send a string to the database and let it deal with it (and let the developer take full responsibility for security), forget it.

Of course you could code an extension to allow that in some way or another, but you just as well off using mysqli to do it and leave Doctrine to its ORM business.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Alexis Finn
  • 129
  • 1
  • 2