Question

Is there a way to add a default scope to a Zend_Db_Table_Abstract based model.

I want to be able to query a model with some conditions taken as default.

e.g.

  • deleted = false
  • order name asc
Was it helpful?

Solution

You can override the Zend_Db_Table_Abstract:: _fetch() method and modify the generated Zend_Db_Table_Select in there before retrieving the rows from the database adapter. As far as I know all fetch*-methods and find() in Zend_Db_Table_Abstract boil down to this generic row-retrieval-method (besides Zend_Db_Table_Abstract::fetchNew() naturally), so your modified code will be called everytime rows are retrieved from the database.

/**
 * Support method for fetching rows.
 *
 * @param  Zend_Db_Table_Select $select  query options.
 * @return array An array containing the row results in FETCH_ASSOC mode.
 */
protected function _fetch(Zend_Db_Table_Select $select)
{
    $select->where('deleted = false')->order('name asc');
    return parent:: _fetch($select);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top