Created
May 15, 2011 00:53
-
-
Save trooney/972785 to your computer and use it in GitHub Desktop.
li3: simple search feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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