Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute raw SQL using Doctrine 2

Tags:

sql

php

doctrine

I want to execute raw SQL using Doctrine 2

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

like image 372
Jiew Meng Avatar asked Jul 24 '10 12:07

Jiew Meng


People also ask

What is doctrine SQL?

Doctrine features a powerful query builder for the SQL language. This QueryBuilder object has methods to add parts to an SQL statement. If you built the complete state you can execute it using the connection it was generated from. The API is roughly the same as that of the DQL Query Builder.

Should I use raw SQL?

Raw SQL is for sure the most powerful way to interact with your database as it is the databases native language. The drawback is that you might use features which are specific to that database, which makes a future database switch harder.

What is a raw query in SQL?

Essentially the raw SQL queries are intended to only be used for the edge cases where the Django ORM does not fulfil your needs (and with each new version of Django it support more and more query types so raw becomes less useful).


1 Answers

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(); }    
like image 159
Jason Swett Avatar answered Sep 17 '22 20:09

Jason Swett