I am trying to achieve the following SQL Query:
SELECT * FROM properties p where location = 'Manchester' AND (proptype LIKE('%House%') OR proptype LIKE('%Bungalow%'));
In my controller I tried the following:
if($_GET['location'][0] != null)
{
$criteria->addInCondition("location", $_GET['location']);
}
if($_GET['prop_type'][0] != null)
{
$count=1;
foreach($_GET['prop_type'] as $value)
{
if($count<=1)
{
$criteria->addSearchCondition("proptype", $value, true);
}
elseif($count>1)
{
$criteria->addSearchCondition("proptype", $value, true, 'OR');
}
$count++;
}
}
This produces the following query:
((location=:ycp0) AND (proptype LIKE :ycp1)) OR (proptype LIKE :ycp2) [params] => Array ( [:ycp0] => Manchester [:ycp1] => %House% [:ycp2] => %Bungalow% )
This provides incorrect results. Notice how ‘location’ and the first ‘proptype’ have been grouped together. Can anyone advise me on how to do this?
Essentially I’m trying to combine SQL IN and LIKE functionality.