tags:

views:

28

answers:

2

Before I retrieve data I always have to type:

$STH->setFetchMode(PDO::FETCH_OBJ);

In the interest of making my code more readable it would be great if I could set a default mode somewhere....

Thanks!

Edit. I was originally hoping I could add PDO:FETCH_OBJ to the setAttribute code I run when I connect to the DB, but that doesn't seem to work...

+1  A: 

I'm not sure if there is a built-in method for setting a default fetch mode - but there are a couple of options that you have to accomplish this:

1) Write a new class that extends the PDO class and overrides the prepare and query methods and calls setFetchMode on the PDOStatement object before returning it.

2) You can specify the fetch mode when calling fetch. You can set a default constant at the top of your script and then call the default whenever you call fetch ($STH->fetch(PDF::FETCH_OBJ))

thetaiko
Thanks for your answer!
Matt
+1  A: 

No, there isn't a built in method. But there is a request for it.

So you probably will need to build a wrapper around PDO (or extend PDO itself).

PS: You may specify the fetch type when calling query. That saves you the setFetchMode line ;)

nikic
Thanks for your answer. I would have marked them both as the answer if I could have!
Matt