问题
I'm using cakephp 2.1 and cakedc search plugin.
The thing is I can't glue it together. Also got:
Notice (8): Indirect modification of overloaded property ProjectsController::$paginate has no effect [APP\Controller\ProjectsController.php, line 48]
Followed cakedc tutorial but something's missing! Simple search just won't filter anything.
I want to filter by name field.
On my projects model
public $actsAs = array('Search.Searchable');
var $name = 'Project';
public $filterArgs = array(
array('name' => 'name', 'type' => 'like'),
array('name' => 'filter', 'type' => 'query', 'method' => 'orConditions'),
);
public function orConditions($data = array()) {
$filter = $data['filter'];
$cond = array(
'OR' => array(
$this->alias . '.name LIKE' => '%' . $filter . '%',
//$this->alias . '.body LIKE' => '%' . $filter . '%',
));
return $cond;
}
in my controller:
public $components = array('Search.Prg');
public $presetVars = array(
array('field' => 'name', 'type' => 'value')
);
index function updated to use index.ctp only (no find function)
public function index() {
$this->Prg->commonProcess();
$this->Project->recursive = 0;
// next line causes
// Notice (8): Indirect modification of overloaded property ProjectsController::$paginate has no effect [APP\Controller\ProjectsController.php, line 48]
//$this->paginate['conditions'] = $this->Project->parseCriteria($this->passedArgs);
$this->set('projects', $this->paginate());
}
and added search form to view.ctp
echo $this->Form->create('Project', array('url' => array_merge(array('action' => 'index'), $this->params['pass'])));
echo $this->Form->input('name', array('div' => false));
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
I know this must be some obvious error on my part, please bear with me. Can anyone help?
Thanks a lot !
回答1:
I'm glad to let people know I found my way to use cakedc search plugin.
First I had to closely follow this tutorial for 1.3 (at first I thought it won't do for 2.1, but it works like charm.)
http://www.youtube.com/watch?v=FAVuLXFVaCw
AND downloaded 1.3 sample code from cakedc
http://cakedc.com/eng/downloads/view/cakephp_search_plugin
to make sense of video tutorial. (tried to run it following readme instructions but got error Fatal error: Class 'Dispatcher' not found in C:\wamp\www\search\webroot\index.php on line 83 so
chose to just get code snippets and have them work in 2.1)
Back to my project:
1.- Downloaded search version 2.1 from cakedc https://github.com/CakeDC/search,
file CakeDC-search-2.1-0-g834f79f.zip.
2.- Placed all files on /plugins/Search/ folder
3.- Added
CakePlugin::load('Search');
to the bottom of /Config/bootstrap.php
4.- On my controller, declared the component and presetVars (I'm using a field called name)
public $components = array('Search.Prg');
public $presetVars = array(
array('field' => 'name', 'type' => 'value'),
array('field' => 'pr_status', 'type' => 'value'),
);
5.- and updated my index function:
public function index() {
$this->Prg->commonProcess();
$this->paginate = array(
'conditions' => $this->Project->parseCriteria($this->passedArgs));
$this->set('projects', $this->paginate());
}
6.- on my model, added
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
array('name' => 'name', 'type' => 'query', 'method' => 'filterName'),
array('name' => 'pr_status', 'type' => 'value'),
);
public function filterName($data, $field = null) {
if (empty($data['name'])) {
return array();
}
$nameField = '%' . $data['name'] . '%';
return array(
'OR' => array(
$this->alias . '.name LIKE' => $nameField,
));
}
// Built a list of search options (unless you have this list somewhere else)
public function __construct($id = false, $table = null, $ds = null) {
$this->statuses = array(
'' => __('All', true),
0 => __('Bid', true),
1 => __('Cancelled', true),
2 => __('Approved', true),
3 => __('On Setup', true),
4 => __('Field', true),
5 => __('Closed', true),
6 => __('Other', true));
parent::__construct($id, $table, $ds);
}
7.- Lastly, created the search form on index.ctp, right above the table tag
<div><?php
echo $this->Form->create('Project', array(
'url' => array_merge(array('action' => 'index'), $this->params['pass'])
));
echo $this->Form->input('name', array('div' => false, 'empty' => true)); // empty creates blank option.
echo $this->Form->input('pr_status', array('label' => 'Status', 'options' => $statuses));
echo $this->Form->submit(__('Search', true), array('div' => false));
echo $this->Form->end();
?>
</div>
I also learned not to discard 1.3 tutorials and documentation even though i'm using 2.1. Next step is to filter by dates and a nicer search form.
Good luck everybody.
Carlos
来源:https://stackoverflow.com/questions/10590199/cakephp-basic-help-to-use-cakedc-search-plugin