I want to execute raw SQL using Doctrine 2
I need to truncate the database tables and initialize tables with default test data.
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.
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.
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).
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With