Zend_Db_Select and Zend_Db_Table_Select are great ways to solve your problem, a problem that I had to deal with only six months ago.
Suppose I have a book table in my database, and I defined a search function to allow me to perform a full-text search on all fields of the table:
Default_Model_DbTable_Books.php:
public class Default_Model_DbTable_Books extends Zend_Db_Table_Abstract {
public function search($query) {
$select = $this->select(true);
if (isset($query) && is_string($query)) {
$db = $this->getAdapter();
$where = $db->quoteInto('isbn LIKE ? OR name LIKE ? OR author LIKE ?', $query);
$search->where($where)
->order('date_published DESC')
->group('author');
}
return $select;
}
}
Default_Model_Mapper_Book.php:
public class Default_Model_Mapper_Book {
public function search($query) {
return $this->getDbTable()->search($query);
}
}
Default_BooksController.php:
public function listAction() {
$mapper = new Default_Model_Mapper_Book();
$select = $mapper->search($this->_getParam("query"));
$paginator = Zend_Paginator::factory($select);
}
source
share