Skip to content

Instantly share code, notes, and snippets.

@trooney
Created May 15, 2011 00:53
Show Gist options
  • Save trooney/972785 to your computer and use it in GitHub Desktop.
Save trooney/972785 to your computer and use it in GitHub Desktop.
li3: simple search feature
<?php
namespace app\controllers;
use lithium\data\Entity;
use app\models\Companies;
class CompaniesController extends \lithium\action\Controller {
public function search() {
// Entity stores data from any POST requests
$search = new Entity(array('data' => $this->request->data));
// Use the model schema to create a list of searchable fields
$fields = array();
foreach (Companies::schema() as $fieldKey => $fieldSchema) {
$fields[$fieldKey] = \lithium\util\Inflector::humanize($fieldKey);
}
// Create query conditions from the search data
$conditions = array();
if ($search->field && $search->query) {
$conditions += array($search->field => array('LIKE' => "%{$search->query}%"));
}
// Find any records
$companies = Companies::find('all', compact('conditions'));
return compact('search', 'fields', 'companies');
}
}
?>
<fieldset>
<legend>Search</legend>
<?= $this->form->create($search); ?>
<?= $this->form->field('field', array('type' => 'select', 'list' => $fields)); ?>
<?= $this->form->field('query'); ?>
<?= $this->form->submit('Search'); ?>
<?= $this->form->end(); ?>
</fieldset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment