views:

142

answers:

2

If I use an ORM let's say with Zend or Symfony. Is it an all or nothing deal?

I'd like to use the ORM, but also want to optimize performance in some cases and write the query myself to get to the nitty gritty. So if I start using an ORM, is it going to be difficult to do it the old way once I include it in my project?

+8  A: 

Most ORMs will let you run adhoc queries.

p.campbell
`Which ORMs were you considering?` I'm looking at propel and doctrine, at least for now.
jblue
Zend has the ability to grab the db handle and just fire raw queries at the db also. It's more important that you put all your db access code into model classes so you can find and update it later than it is to purely stick to the orm in my opinion.
Matt Wheeler
+6  A: 

Using Doctrine it is fairly easy to "break out" of the ORM. Doctrine lets you write queries in 4 different ways:

  • DQL. Doctrine's own query language that comes with all of the benefits of Doctrine.
  • "raw" DQL ("Native queries" in Doctrine2). This is similar to DQL but allows a little more flexibility in commands (e.g. database specific features). In this mode, you'll have to specify a little more about how components are related to one another.
  • SQL, using PHP's PDO. You can use a Doctrine_Connection to get a PDO instance which lets you write queries but still have the added safety and ease of use granted by PDO.
  • raw SQL. While I'm not sure why you'd want it, I think Doctrine provides this, if not, you could always break out of Doctrine entirely.

If you're using Doctrine inside of Symfony, there are absolutely no features of Symfony that lock you into using Doctrine, even if it's enabled.

One final warning: if you're using some of Doctrine's advanced features (e.g. events or behaviors) these will become difficult to tie in when you do queries outside of DQL.

jeremy
`events or behaviors` you mean like `created_at` and `updated_at`?
jblue
If you're using Doctrine's `Timestampable` behavior, it wouldn't manage records inserted outside of Doctrine. However, Doctrine doesn't stop you from managing timestamps however you would outside of Doctrine to begin with.
jeremy