Page 1 of 1

Zend_Db_Table - I can't figure out how to select w/OR

Posted: Tue Oct 16, 2007 1:30 pm
by Luke
I'm trying to make a search by field / search by all fields advanced search function. Here is the method on my model:

Code: Select all

public function search($type = null, $query = null)
    {
        // you can't sort by "all" so sort by name if that is the type
        $order = ($type != 'all') ? $type : 'name'; 
        if (is_null($query)) $query = '';
        $where = array(); // when this is empty, the fetchAll below will fetch "ALL"
        
        $searchtypes = array(
            'name' => array(
                'where' => 'name LIKE ?',
                'values' => "%" . $query . "%"
            ),
            'city' => array(
                'where' => 'city LIKE ?',
                'values' => $query
            ),
            'state' => array(
                'where' => 'state LIKE ?',
                'values' => $query . "%"
            ),
            'zip_code' => array(
                'where' => 'zip_code LIKE ?',
                'values' => $query . "%"
            )
        );
        
        foreach ($searchtypes as $searchtype => $search)
        {
            if (in_array($searchtype, array('all', $type)))
            {
                $where[] = $this->getAdapter()->quoteInto($search['where'], $search['values']);
            }
        }
        
        $return = $this->fetchAll($where, $order);
        return $return->toArray();
    }
When the search type is "all", it adds all of the $where's to the query, but it joins them with an "AND". How to I make that an "OR". The Zend docs have been NO help on this. :(

Posted: Tue Oct 16, 2007 1:59 pm
by Christopher
Never has so much code done so little for so many ...

The direct way would be something like:

Code: Select all

$this->_db->select()->orWhere("zip_code LIKE '$query'");
But I recall that there is a way to do it with the array of conditions too .... array of arrays? I forget.