ZF-3355: Implement fetchPairs()

Description

Fetching key => value pairs is a common need for every one. Zend_Db_Adapter_Abstract has a convenient fetchPairs() method, and this issue aims to add even more convenient support for this in Zend_Db_Table.

Comments

A patch is attached. It works exactly the same as fetchAll(), but it accepts an extra parameter (the first) which has to be an array of the 2 column names.

Usage of $where, $order, $count and $offset parameters to modify the query is depracated in fetchRow() and fetchAll() methods. Wouldn't it make sense for new method to allow passing only Zend_Db_Table_Select instance as no BC break can happen?

Please assign and categorize as necessary. I've already targeted a release, but please update if that schedule is not realistic.

I totally agree with Martin, but that's in the case it will be scheduled for next Major release. (Which is how it's scheduled now).

But if it's going to be in the next minor release, then IMO it should try to be consistent with the rest of the usual fetch* API. Otherwise it would feel like PHP, have to go look up the manual every time... :P

I've attached a patch which accepts Zend_Db_Table_Select instances only.

Working patch is ready for over a year, but not yet in upstream :-( ? This method is quite handy in many cases (e.g. adding multi options to zend_form_element_select), and current state force developers to apply this patch on their own extended classes, or break the zend_db_table paradigm and use directly zend_db->fetchPairs()...

So please, if this patch is ok, would be possible to merge it into zend_db_table?

BTW, instead of direct access to $this->_rowsetClass, the getter class should be used, like this:

Patch for Zend_Db_Table_fetchPairs.patch:

  • Zend_Loader::loadClass($this->_rowsetClass);
  • return new $this->_rowsetClass($data);
  • $rowsetClass = $this->getRowsetClass();
  • if (!class_exists($rowsetClass)) {
  • require_once 'Zend/Loader.php';
  • Zend_Loader::loadClass($rowsetClass);
  • }
  • return new $rowsetClass($data);

Ehm, maybe this one gets better comments:


-        Zend_Loader::loadClass($this->_rowsetClass);
-        return new $this->_rowsetClass($data);
+        $rowsetClass = $this->getRowsetClass();
+        if (!class_exists($rowsetClass)) {
+            require_once 'Zend/Loader.php';
+            Zend_Loader::loadClass($rowsetClass);
+        }
+        return new $rowsetClass($data);