Dynamic Letter Filter for a Kohana Controller Object

December 26, 2012

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;
	}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive