Can I make PDOStatement->fetchObject not use non-member variables?
Posted
by threendib
on Stack Overflow
See other posts from Stack Overflow
or by threendib
Published on 2010-05-29T00:41:27Z
Indexed on
2010/05/29
0:42 UTC
Read the original article
Hit count: 238
Lets say I have a class like this:
Class User {
var $id
var $name;
}
And I run a query using PDO in php like so:
$stm = $db->prepare('select * from users where id = :id');
$r = $stm->execute(array(':id' => $id));
$user = $r->fetchObject('User');
If I vardump my user object it has all kinds of other fields in it that I have not defined in the User class. Obviously I could make my query specific so that it only gives me back the fields I need/want. But if I don't want to do that is there any way to make this work the way I want it to?
I like the idea of fetchObject, because it's one line of code to create this object and set member variables for me. I just don't want it to set variables I haven't defined in my class.
© Stack Overflow or respective owner