Search code examples
phpzend-frameworkzend-dbzend-db-table

Zend_Db custom query to db rows instead of array


When I am callin fetchAll() on my DbTable I get results in proper DbRow classes defined in DbTable.

But when I create custom query like this I get results in array. Is there any parameter that can force receiving this data ind DbRows or I should create rows by myself and populate them with those arrays?

$query = $this->_dbTable->getDefaultAdapter()->select()
        ->from('doctor.doctor')
        ->joinInner('facility.doctorfacility', 'facility.doctorfacility.doctor_id = doctor.doctor.id')
        ->joinInner('facility.facility', 'facility.doctorfacility.facility_id = facility.facility.id')
        ->where(implode(' AND ', $conds));

return $this->_dbTable->getDefaultAdapter()->fetchAll($query);

Solution

  • "But when I create custom query like this I get results in array"

    You are getting an array because you are calling Zend_Db_Adapter_Abstract::fetchAll() which according to the docblock in the code returns an array:-

    /**
     * Fetches all SQL result rows as a sequential array.
     * Uses the current fetchMode for the adapter.
     *
     * @param string|Zend_Db_Select $sql  An SQL SELECT statement.
     * @param mixed                 $bind Data to bind into SELECT placeholders.
     * @param mixed                 $fetchMode Override current fetch mode.
     * @return array
     */
    public function fetchAll($sql, $bind = array(), $fetchMode = null)
    

    "When I am callin fetchAll() on my DbTable I get results in proper DbRow classes defined in DbTable."

    When you do this you are calling Zend_Db_Table_Abstract::fetchAll() which according to the docblock in the code returns a Zend_Db_Table_Rowset:-

    /**
     * Fetches all rows.
     *
     * Honors the Zend_Db_Adapter fetch mode.
     *
     * @param string|array|Zend_Db_Table_Select $where  OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
     * @param string|array                      $order  OPTIONAL An SQL ORDER clause.
     * @param int                               $count  OPTIONAL An SQL LIMIT count.
     * @param int                               $offset OPTIONAL An SQL LIMIT offset.
     * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
     */
    public function fetchAll($where = null, $order = null, $count = null, $offset = null)
    

    "Is there any parameter that can force receiving this data ind DbRows or I should create rows by myself and populate them with those arrays?"

    No there isn't but if you call the correct method on the correct object you will get your rowsets returned.

    To do this change this line:-

    $query = $this->_dbTable->getDefaultAdapter()->select()
    

    To:-

    $query = $this->_dbTable->select()
    

    An this line:-

    return $this->_dbTable->getDefaultAdapter()->fetchAll($query);
    

    To:-

    return $this->_dbTable->fetchAll($query);
    

    That should get you what you need. It is always worthwhile looking at the code if you are stuck in ZF, it is by far the best documentation available.