Zend Framework Forum

Go Back   Zend Framework Forum > Zend Framework Components > Databases

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-27-2009, 02:23 PM
Junior Member
 
Join Date: Feb 2009
Posts: 3
Default FetchAll as typed objects

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2009, 02:25 PM
Senior Member
 
Join Date: Sep 2008
Location: Croatia
Posts: 356
Send a message via MSN to Eugen
Default

RTFM
Zend Framework: Documentation
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2009, 03:45 PM
Junior Member
 
Join Date: Feb 2009
Posts: 3
Default

Quote:
Originally Posted by Eugen View Post
I allready have the set the setFetchMode to Zend_Db::FETCH_OBJ. Which indeeds makes the result set an array of objects. But I can't specify what kind of object to use.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-02-2009, 08:04 AM
Senior Member
 
Join Date: Sep 2008
Location: Croatia
Posts: 356
Send a message via MSN to Eugen
Default

Quote:
Originally Posted by rsids View Post
I allready have the set the setFetchMode to Zend_Db::FETCH_OBJ. Which indeeds makes the result set an array of objects. But I can't specify what kind of object to use.
? what do you mean by what kind of object?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-02-2009, 09:48 AM
Junior Member
 
Join Date: Feb 2009
Posts: 3
Default

Quote:
Originally Posted by Eugen View Post
? what do you mean by what kind of object?
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-02-2009, 10:05 AM
Senior Member
 
Join Date: Sep 2008
Location: Croatia
Posts: 356
Send a message via MSN to Eugen
Default

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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-02-2009, 05:32 PM
SirAdrian's Avatar
Member
 
Join Date: Apr 2008
Posts: 83
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-04-2009, 10:42 PM
Junior Member
 
Join Date: May 2009
Posts: 2
Unhappy It should work like PDO.....

FetchAll should work as PDO:

PHP Tutorials Examples Introduction to PHP PDO

Quote:
"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');
"
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-05-2009, 08:53 AM
Junior Member
 
Join Date: Feb 2009
Posts: 8
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-06-2009, 11:58 AM
Junior Member
 
Join Date: May 2009
Posts: 2
Exclamation VO approach

Quote:
Originally Posted by stoot98 View Post
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

Quote:
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;        
#
   }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT. The time now is 12:31 AM.


Designed by: Miner Skinz Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0