Pdo fetch object?
Is there a better way to return a db result object?
I know " return $statement->fetchall(PDO::FETCH_CLASS, 'modelClassName'); " but it only works on the fetchall function?
class modelArea extends Model { public $areaID; public $postcode; public static function find($condition, $parameters) { $query = "SELECT * FROM area WHERE " . $condition; $statement = self::getDb()->prepare($query); if (!$statement->execute($parameters)) return false; $query = $statement->fetch(PDO::FETCH_ASSOC); $a = new modelArea(); $a->areaID = $query['areaID']; $a->postcode = $query['postcode']; return $a; } } The PDOStatement::fetchObject does what you want.
You can use it like:
$a = $statement->fetchObject('modelArea'); This will cause $a be an object of class modelArea , equivalent to the code you give.