开发者

zend framework 1.11.5 is choking on search function - mysql db

开发者 https://www.devze.com 2023-03-10 20:25 出处:网络
ZF 1.11.5 is puking all over this search function.i\'ve tried creating the query several different ways, sent the sql statement开发者_运维问答 to my view, copied and pasted the sql statement into phpM

ZF 1.11.5 is puking all over this search function. i've tried creating the query several different ways, sent the sql statement开发者_运维问答 to my view, copied and pasted the sql statement into phpMyAdmin and successfully retrieved records using the sql that ZF is choking on. i have been getting a coupld of different errors: 1) an odd SQL error about 'ordinality' (from my Googling ... it seems this is a ZF hang up .. maybe?) and 2) Fatal error: Call to undefined method Application_Model_DbTable_Blah::query() in /blah/blah/blah.php on line blah

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

here's my model ....

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

never mind the view file ... that never throws an error. on a side note: i'm seriously considering kicking ZF to the curb and using CodeIgniter instead.

looking forward to reading your thoughts. thanks ( in advance ) for your responses


You're trying to all a method called query() on Zend_Db_Table but no such method exists. Since you have built the SQL already you might find it easier to call the query on the DB adapter directly, so:

$selectSql = "SELECT * FROM listings WHERE `s_coursedesc` LIKE '%".$searchTerm."%' || `s_title` LIKE '%".$searchTerm."%'";
$searchResults = $selectQuery->getAdapter()->fetchAll($selectSql);

but note that this will give you arrays of data in the result instead of objects which you might be expecting. You also need to escape $searchTerm here since you are getting that value directly from POST data.

Alternatively, you could form the query programatically, something like:

$searchTerm = '%'.$searchTerm.'%';
$select = $selectQuery->select();
$select->where('s_coursedesc LIKE ?', $searchTerm)
       ->orWhere('s_title LIKE ?', $searchTerm);
$searchResults = $searchQuery->fetchAll($select);
0

精彩评论

暂无评论...
验证码 换一张
取 消