Zend_Db_Table_Abstract in version 1.5

Zend_Db_Table has been significantly enhanced for version 1.5, mainly by Simon Mundy and is really really good. It's going to take me a while to poke around and find out all the new nuances!

However, I did find one gotcha that has affected me on my main project at work.

If you happen to have extended Zend_Db_Table_Abstract::_fetch() for some reason or another, be aware that it's changed in version 1.5.

It was:

PHP:
  1. /**
  2. * Support method for fetching rows.
  3. *
  4. * @param  string|array $where  OPTIONAL An SQL WHERE clause.
  5. * @param  string|array $order  OPTIONAL An SQL ORDER clause.
  6. * @param  int          $count  OPTIONAL An SQL LIMIT count.
  7. * @param  int          $offset OPTIONAL An SQL LIMIT offset.
  8. * @return array The row results, in FETCH_ASSOC mode.
  9. */
  10. protected function _fetch($where = null, $order = null, $count = null, $offset = null)
  11. {
  12.     // lots of stuff
  13. }


and is now:

PHP:
  1. /**
  2. * Support method for fetching rows.
  3. *
  4. * @param  Zend_Db_Table_Select $select  query options.
  5. * @return array An array containing the row results in FETCH_ASSOC mode.
  6. */
  7. protected function _fetch(Zend_Db_Table_Select $select)
  8. {
  9.     // much less stuff
  10. }


Just a heads-up in case someone else has customised this function for their own needs.

Related to this, Ralph Eggert recently discovered something else related 1.5 changes when extending Zend_Db_Table_Abstract and Simon Mundy helped out. The select object that is the parameter to _fetch() has a check in it to prevent you joining to another table. This is because if you join, you break the concept that a Zend_Db_Table represents a single table. However, you can get around it using the function setIntegrityCheck() like this:

PHP:
  1. protected function _fetch(Zend_Db_Table_Select $select)
  2. {
  3.     // extend select
  4.     $select->setIntegrityCheck(false);
  5.     $select->join(// do stuff);
  6. }


The setIntegrityCheck() statement allows you to do a join in the select object and very cleverly puts the table object into read only mode, which is great.

Also, Example 10.136 of the Zend_Db_Table documentation has more information on how to use the $select to get back the data you actually want.

Posted by Rob on 30th January 2008 under Tips and Tricks |

4 Responses to “Zend_Db_Table_Abstract in version 1.5”

  1. Erik responded on 31 Jan 2008 at 6:49 am #

    Thanks for the heads up. I ran into this problem the other day, this should save me some time. :)

  2. Sébastien responded on 31 Jul 2008 at 8:33 am #

    Hello,

    I need to “fetchAll” data from a table encapsulating “date”,”datetime” fields into mysql UNIX_TIMESTAMP function.

    With ZF 1.x I replace these fields by a Zend_Db_Expr

    … public function __construct() { parent::__construct();

        $this->_cols['fecha']=new Zend_Db_Expr('UNIX_TIMESTAMP(fecha)');
        $this->_cols['fechanacimiento']=new Zend_Db_Expr('UNIX_TIMESTAMP(fechanacimiento)');
        $this->_cols['umodificacion']=new Zend_Db_Expr('UNIX_TIMESTAMP(umodificacion)');
    
    }
    

    It’s totaly ignoered witht ZF 1.5.x … I’ve tried to use _setupMetadata() method but it’s ignored too …

    Any Ideas ? Thank you

    Regards

  3. Rob responded on 02 Aug 2008 at 6:12 pm #

    Hi,

    Have you asked on the mailing list?

    Regards,

    Rob…

  4. Sébastien responded on 03 Aug 2008 at 6:45 am #

    I’ve got some problem to subscribe to the ZF mailing lists. I don’t know why… It’s very strange seems that the problem is that is uses a Zend_Db_Select object instead of the SQL query of the version 1.x … If I overload the _fetch method of Zend_Db_Table concatening inside the extra fields with the current _cols vars and $select->from($this->_table, $cols) … it works ! … It’s not very elegant but It works … I’m searchig another workaround to solve this issue.

Comments RSS

Leave a Reply