Hi,
Is it possible to do a fetchAll with a specified classname as resultobject?
For a single row, I can do this:
[PHP]$users = new Users();
$select = $users -> select();
$select -> from("users", Array('userId', 'username', 'fullname', 'email', 'lastlogin', 'isadmin'));
$stmt = $this -> db -> query($select);
$user = $stmt -> fetchObject("User");
return $user;[/PHP]
But I want to have a list of users, not a single user.
So something like this:
[PHP]
$stmt = $this -> db -> query($select);
$user = $stmt -> fetchAll("User");[/PHP]
The reason I want to have this is because I want (need) classmapping between my Flex application and PHP.
Please help!
If want to specify a class as objecttype, instead of php's standard StdClass.
my class looks something like this:
[PHP]
class User {
var $_explicitType = "User";
public $userId = 0;
public $username = "";
public $password = "";
public $fullname = "";
public $email = "";
public $lastlogin = "";
public $isadmin = false;
}[/PHP]
So when I do a fetchAll, I want an array of User classes.
i think you can't specify it, but take a look at fetchAll method.. you can always extend it so it will except class name...
I use a fetchResults method in my models that replaces $db->fetchAll (calls it internally), for adding in things like pagination handling, converting to objects, etc.
AFAIK there's no way to do it automatically.
FetchAll should work as PDO:
PHP Tutorials Examples Introduction to PHP PDO
"FETCH CLASS
PDO::FETCH_CLASS instantiates a new instance of the specified class. The field names are mapped to properties (variables) within the class called. This saves quite a bit of code and speed is enhanced as the mappings are dealt with internally.
"Code:/*** fetch into the animals class ***/ $obj = $stmt->fetchALL(PDO::FETCH_CLASS, 'animals');
Or if your using a Zend_Db_Table_Abstract object to do the query you can set the protected propert "$_rowClass" and "$_rowsetClass" which will mean object(s) of that type are returned assuming they extend the Zend_Db_row_Abstract (i think) class.
finally I had to use the "Value Object" approach as noticed here:
Utilizing the Zend AMF Server Inside a Zend Controller | blog.log2e.com
Let’s assume we set up a database adapter in the bootstrap file. This allows us to write very clean code inside the service classes. The following code is just an example (where ArticleVO is a typical value object, and Articles is supposed to be a Zend model class that extends Zend_Db_Table):
Code:# public function getArticles() # { # $articles = array(); # $table = new Articles(); # $result = $table->fetchAll(); # foreach ($result as $row) # { # $article = new ArticleVO(); # $article->id = $row->id; # $article->author = $row->author; # $article->title = $row->title; # $article->teaser = $row->teaser; # $article->body = $row->body; # array_push($articles, $article); # } # return $articles; # }