Dynamic Letter Filter for a Kohana Controller Object

Sarah Berg

Basically this is done with the “like” limiting SQL command. In Kohana, looking for users with names beginning with an “a” would be:

ORM::factory('user')->where('name','LIKE',"a%")->find_all();

So we made a dynamic function in the controller that would retreive the users of a given letter.

$users = ORM::factory('company')->getUsers($letter);

This function can be placed in the the model of the controller, “$letter” can also be a substring:

public function getUsers($letter = '')
	{
		$letter = $letter."%";
		if($letter==""){
			$returnObj = ORM::factory('user')->find_all();
		}
		else{
			$returnObj = ORM::factory('user')->where('name','LIKE',$letter)->find_all();
		}
		return $returnObj;
	}

Last updated by on .

What Are Your Thoughts?

Your email address will not be published. Required fields are marked *