ZF 1.11.5正在考虑这个搜索功能。我尝试了几种不同的方式创建查询,将sql语句发送到我的视图,将sql语句复制并粘贴到phpMyAdmin中,并使用ZF正在阻塞的sql成功检索记录。我得到了一个不同错误的偶然:1)关于'常规'的一个奇怪的SQL错误(来自我的谷歌搜索......似乎这是ZF挂断......也许?)和2)致命错误:调用未定义的方法在/blah/blah/blah.php上的Application_Model_DbTable_Blah :: query()线上等等
public function searchAction($page=1)
{
$searchForm = new Application_Model_FormIndexSearch();
$this->view->searchForm = $searchForm;
$this->view->postValid = '<p>Enter keywords to search the course listings</p>';
$searchTerm = trim( $this->_request->getPost('keywords') );
$searchDb = new Application_Model_DbTable_Ceres();
$selectSql = "SELECT * FROM listings WHERE `s_coursedesc` LIKE '%".$searchTerm."%' || `s_title` LIKE '%".$searchTerm."%'";
$selectQuery = $searchDb->query($selectSql);
$searchResults = $selectQuery->fetchAll();
}
这是我的模特......
class Application_Model_DbTable_Ceres extends Zend_Db_Table_Abstract
{
protected $_name = 'listings';
function getCourse( $courseId )
{
$courseid = (int)$courseId;
$row = $this->fetchRow('id=?',$courseId);
if (!$row)
throw new Exception('That course id was not found');
return $row->toArray();
}
}
不要介意视图文件......从不会抛出错误。旁注:我正在认真考虑将ZF踢到路边并使用CodeIgniter代替。
期待阅读你的想法。感谢(提前)您的回复
答案 0 :(得分:1)
您正尝试在Zend_Db_Table上调用所有名为query()
的方法,但不存在此类方法。由于您已经构建了SQL,因此您可能会更容易直接在DB适配器上调用查询,因此:
$selectSql = "SELECT * FROM listings WHERE `s_coursedesc` LIKE '%".$searchTerm."%' || `s_title` LIKE '%".$searchTerm."%'";
$searchResults = $selectQuery->getAdapter()->fetchAll($selectSql);
但请注意,这将为您提供结果中的数据数组,而不是您可能期望的对象。您还需要在此处转义$ searchTerm,因为您直接从POST数据获取该值。
或者,您可以以编程方式形成查询,例如:
$searchTerm = '%'.$searchTerm.'%';
$select = $selectQuery->select();
$select->where('s_coursedesc LIKE ?', $searchTerm)
->orWhere('s_title LIKE ?', $searchTerm);
$searchResults = $searchQuery->fetchAll($select);