addSearchCondition for multiple values

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.

Anyone know how I can do this?

No, but perhaps this can help you come closer to a solution, one way or another

Issue with mergeWith

/Tommy

Cheers. I’ve had to do this manually to make it work for me.

I think a new function needs to be introduced to deal with this requirement.

This is how I managed to do it:




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)

		{

			$statement="'%".$value."%'";

		}

		if($count>1)

		{

			$statement.= " OR proptype LIKE '%".$value."%'";

		}

		$count++;

	}

	$criteria->addCondition("proptype LIKE {$statement}");

}



This works, but it’s more of a hack. Does anyone know of a way to do it using the built in functions of Yii?