Zend_Db_Select和Zend_Db_Table_Select是解決您的問題的好方法,這個問題也是我六個月前不得不面對的問題。
假設我有書的表在我的數據庫,我定義的搜索功能,讓我整個表的字段做全文搜索:
Default_Model_DbTable_Books.php:
public class Default_Model_DbTable_Books extends Zend_Db_Table_Abstract {
// Other variables and methods here...
public function search($query) {
// Initialize Zend_Db_Table_Select object
$select = $this->select(true);
if (isset($query) && is_string($query)) {
// Logic for search (don't actually do this, it's horrible for performance)
$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 {
// Defined functions as per Zend Quickstart Mapper classes...
public function search($query) {
// Returns Zend_Db_Table_Select object
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);
}
它的像這樣的事情,提醒我,爲什麼我不使用Zend的... – ircmaxell 2010-11-11 17:43:18
@ircmaxell Zend公司是好的,但它需要一些工作,弄清楚它的錯綜複雜。實際上,您可以製作非常優雅的代碼,而不必訴諸許多類擴展和覆蓋。 – 2010-11-19 20:39:46