Zend_Db_Table - I can't figure out how to select w/OR
Posted: Tue Oct 16, 2007 1:30 pm
I'm trying to make a search by field / search by all fields advanced search function. Here is the method on my model:
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. 
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();
}