+ Reply to Thread
Results 1 to 10 of 10

Thread: FetchAll as typed objects

  1. #1
    rsids is offline 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!

  2. #2
    Eugen is offline Senior Member
    Join Date
    Sep 2008
    Location
    Croatia
    Posts
    400

  3. #3
    rsids is offline 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.

  4. #4
    Eugen is offline Senior Member
    Join Date
    Sep 2008
    Location
    Croatia
    Posts
    400

    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?

  5. #5
    rsids is offline 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.

  6. #6
    Eugen is offline Senior Member
    Join Date
    Sep 2008
    Location
    Croatia
    Posts
    400

    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...

  7. #7
    SirAdrian's Avatar
    SirAdrian is offline Member
    Join Date
    Apr 2008
    Posts
    87

    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.

  8. #8
    dersteppenwolf is offline 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

    "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');
    "

  9. #9
    stoot98 is offline 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.

  10. #10
    dersteppenwolf is offline 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

    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;        
    #
       }

+ Reply to Thread

Similar Threads

  1. How does ZF work? View objects...
    By ajlisowski in forum General Q&A on Zend Framework
    Replies: 1
    Last Post: 06-14-2010, 03:54 PM
  2. Controller - Objects
    By RPTNNN in forum Model-View-Controller (MVC)
    Replies: 3
    Last Post: 12-09-2009, 07:26 AM
  3. Returning array of objects ?
    By davidmpaz in forum Web & Web Services
    Replies: 0
    Last Post: 05-23-2009, 02:15 PM
  4. Two Zend_Db_Select objects and UNION
    By risoknop in forum Databases
    Replies: 2
    Last Post: 03-24-2009, 04:10 PM
  5. Zend_Amf Exception: RemoteClass Typed object not implmented
    By ternur in forum Web & Web Services
    Replies: 0
    Last Post: 09-21-2008, 03:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts