O'Reilly logo

Persistence in PHP with Doctrine ORM by Kévin Dunglas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing native queries

In the previous chapter, we learned how to create DQL queries through the QueryBuilder. But DQL has some limitations (that is, queries cannot contain subqueries in FROM and JOIN clauses), and sometimes you want to use specific features of your DBMS (that is, MySQL full-text search). In such cases you need to write native SQL queries.

The NativeQuery class

The NativeQuery class allows you to execute native SQL queries and to get their results as Doctrine entities. Only SELECT queries are supported.

To experiment with this feature, we will create a new command that displays the 100 most recent comments. This can be useful to moderate them.

Create a file containing this new command called last-comments.php in the bin/ directory ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required