Thanks so much!!

On Nov 7, 2008, at 8:21 AM, Matthew Weier O'Phinney wrote:

I sent my original reply to you, but I'm posting here for the archives.

-- Edward Haber <[EMAIL PROTECTED]> wrote
(on Friday, 07 November 2008, 07:09 AM -0500):
Thanks for the help! It is much appreciated. I had a feeling I was doing
something a bit fishy.

I tried the code below and the method is returning an associative array instead of an array of Article objects. If I do change the setFetchMode
globally in the bootstrap, it changes to stdClass.

Is there a way I can get this method to return an array of Article
objects?

Aha -- that was the missing bit.

By default, if you call fetchAll() on the _table_ object, it returns a
Zend_Db_Table_Rowset object, which is simply an iterable object of Row
objects. In the class definition you presented, this would mean that it
would return a rowset populated by Article objects. The following
achieves that:

   return $this->fetchAll($select);

Alternately, if you really, really want to have it return an array
instead of a rowset, do the following:

   $rowset = $this->fetchAll($select);
   $return = array();
   foreach ($rowset as $row) {
       $return[] = $row;
   }
   return $return;

Personally, I'd just use the Rowset -- it acts just like an array for
all practical intents and purposes.

On Nov 6, 2008, at 9:35 PM, Matthew Weier O'Phinney wrote:

-- DorkFest <[EMAIL PROTECTED]> wrote
(on Thursday, 06 November 2008, 05:58 PM -0800):
This was posted in Dec 2007. I have the same question. Is there
anyway using
fetchAll and setFetchMode to set the explicit class name that's
returned in
the array?

Or is this a design conflict? Here's basically the situation:

class Articles extends Zend_Db_Table_Abstract
{
        protected $_name = "articles";
        protected $_rowClass = "Article";
        
        public function fetchActive()
        {
                $db = Zend_Registry::get('db');
                $db->setFetchMode(Zend_Db::FETCH_OBJ);
                $select = $db->select()
                        ->from($this->_name)
                        ->where('active = 1')
                        ->order('creation_date DESC');
                // want to return array of articles objects here instead of
stdClass
                return $db->fetchAll($select);
        }
}

class Article extends Zend_Db_Table_Row_Abstract
{
}

Is this the wrong way to approach models? Or is there a better way
to obtain
the fetchAll array of Articles as a return value from the
fetchActive()
method.

Umm... wow... that code is.. wow.

First, when you have a Zend_Db_Table object, you already *have* the
database adapter -- there's no need to grab it from the registry.
Next,
Zend_Db_Table also has its own select object now (since 1.5) that
obviates the need to (a) pull it from the database adapter, and (b)
specify the table name (it's automatically populated). Next,
fetchAll()
on the adapter already returns an array by default -- it has from the
very beginning. You actually have to override this behavior to get a
different return type by passing the fetchmode as the second parameter
to
the call. The reason you're getting anything different is because your
code is calling:

 $db->setFetchMode(Zend_Db::FETCH_OBJ);

which tells the adapter to return fetch results as objects. You're
basically shooting yourself in the foot here.

So, the following will do what you're trying to accomplish:

        public function fetchActive()
        {
                $select = $this->select()
                                   ->where('active = 1')
                                   ->order('creation_date DESC');
                return $this->getAdapter()->fetchAll($select);
        }


Renan Gonçalves wrote:

Hello,

How I can use fetchAll with fethMode = Object and using the class
Article
(for example) ?
I can use fetchObject('Article') and will fetch on my class, but in
fetchAll
the default class is stdClass.

The PDOStatement has the function: (
http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php)
bool *setFetchMode* ( int $PDO::FETCH_CLASS , string $classname ,
array
$ctorargs )

--
Matthew Weier O'Phinney
Software Architect       | [EMAIL PROTECTED]
Zend Framework           | http://framework.zend.com/

Reply via email to