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?
Eddie
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 )
Best Regards,
--
Renan Gonçalves - Web Developer
Cell Phone: +55 (11) 8633-6018
MSN: [EMAIL PROTECTED]
Web Site: renangoncalves.com
São Paulo - SP/Brazil
--
View this message in context:
http://www.nabble.com/Zend_Db-tp14140524p20373369.html
Sent from the Zend Framework mailing list archive at Nabble.com.
--
Matthew Weier O'Phinney
Software Architect | [EMAIL PROTECTED]
Zend Framework | http://framework.zend.com/