Somal Somalski
Somal Somalski

Reputation: 578

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);

Upvotes: 1

Views: 1899

Answers (2)

vascowhite
vascowhite

Reputation: 18440

"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.

Upvotes: 3

Venu
Venu

Reputation: 7279

You cannot force it, as data would be coming from multiple tables, so mapping is not possible with zend_db. Its left for us to map the data to entities.

If you want array of objects, then can change the fetch mode.

Zend_Db::FETCH_OBJ: return data in an array of objects. The default class is the PHP built-in class stdClass. Columns of the result set are available as public properties of the object.

FYI: Fetch Mode

Some framework's like Doctrine, would allow us to provide mapper, where we could pass custom mapper object.

Upvotes: 1

Related Questions