On Fri, Nov 7, 2008 at 2:58 AM, DorkFest <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> 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.

Looks wrong. You are duplicating your effort.

First off, in your bootstrap you can define a Zend_Db object and do
"setFetchMode()" on it. Then you chain it to your
Zend_Db_Table_Abstract extending classes with
Zend_Db_Table_Abstract::setDefaultAdapter(). I think by default
Zend_Db_Table_Abstract will also use a 'db' object saved in your
registry. So there might not be a need to use setDefaultAdapter().

Inside a model (model_Foo extends Zend_Db_Table_Abstract {}), use:

$db = $this->getAdapter();

That is, if you really need a DB object and $this->select(),
$this->fetchAll(), $this->update(), $this->delete(), $this->insert(),
etc. are not enough.

Also, the framework is growing fast. Don't use tutorials from a year
ago when you start now. Use the manual instead:

http://framework.zend.com/manual/en/zend.db.table.html

Till

Reply via email to