在zend Framework中构建搜索查询

Muh*_*qib 2 php mysql zend-framework

我正在使用ZendFrameWork 1.11.5我需要制作一个多表单搜索查询,就像我有5个字段一样

name (text box)
city (dropDown)
zipCode (text box)
type (dropdown)
Run Code Online (Sandbox Code Playgroud)

现在的问题是...在文本框用户可以输入任何它可能是正确的信息或不..请建议我如何建立一个快速查询.我有什么样的选择..我也试过这个但不工作..不给我正确的结果..

Select * from table where type =       '%$pType%'
                OR  sex     LIKE     '%$sex%'
                OR  race    LIKE      '%$race%'
                OR  kind    LIKE     '%$kind%'
                OR  country LIKE      '%$Country%'
                OR  state   LIKE     '%$statesIDs%'
                OR  zipcode LIKE     '%$zip%'";
Run Code Online (Sandbox Code Playgroud)

hun*_*eox 5

这是我的示例代码.您的代码应该取决于您的需求,查询可能会与另一个表连接,或者LIKE您可以使用MATCH...AGAINST更准确的结果.此代码取决于用户输入的每个参数以进行最终查询.

//In DbTable
public function search($params)
{
    $query = $this->select()
                   ->from(
                    array('tbl'=>'table'),
                    array('name','city','zipcode','type')
                    );
    $query = $this->_makeParams($query,$params);
    return $this->fetchAll($query);
}

private function _makeParams($query, $params)
{
    $name = isset($params['name']) ? trim($params['name']) : '';
    $city = isset($params['city']) ? trim($params['city']) : '';
    $zipcode = isset($params['zipcode']) ? trim($params['zipcode']) : '';
    $type = isset($params['type']) ? trim($params['type']) : '';

    if($name!='')
    {
        $name = '%'.$this->quote($name).'%';//quote is my own function
        $query->where("tbl.name LIKE '?'",$name); 
    }

    if($city!='')
    {
        $query->where("tbl.city=?",$city);
    }

    if($zipcode!='')
    {
        $query->where("tbl.zipcode=?",$zipcode);
    }

    if($type!='')
    {
        $query->where("tbl.type=?",$type);
    }

    return $query;

}
Run Code Online (Sandbox Code Playgroud)