我正在制作显示所有预订的视图(我正在预订应用程序)并且有搜索表单。我正在使用CakeDC插件,但它不起作用。
这是我的行动:
public function index() {
if (!App::import('Component', 'Search.Prg')) {
throw new MissingPluginException(array('plugin' => 'Search'));
}
$this->Prg->commonProcess();
//$this->Paginator->settings['conditions'] = $this->Reservation->parseCriteria(/*$this->Prg->parsedParams()*/);
//$this->set('users', $this->Paginator->paginate());
$searchTerm = '';
//$this->Prg->commonProcess($this->modelClass, $this->modelClass, 'search', false);
$by = null;
if (!empty($this->request->params['named']['searchTerm'])) {
$searchTerm = $this->request->params['named']['searchTerm'];
$by = 'any';
}
$this->request->data[$this->modelClass]['searchTerm'] = $searchTerm;
$this->set('reservation_mode', Configure::read('webakis.reservation_type'));
$this->Reservation->recursive = 0;
$this->paginate = array(
'fields' => array('Reservation.id', 'Reservation.start', 'Reservation.ucode', 'Reservation.ticket_nr', 'Location.name', 'User.username', 'User.id', 'Opmenu.name', 'Queue.name'),
'conditions' => array(
// 'ucode <>' => 0,
'user_id <>' => 0,
'deleted' => 0,
// 'start >=NOW() '
),
'order' => array(
'Reservation.start' => 'asc'
),
//'search',
'limit' => 12,
'by' => $by,
'search' => $searchTerm,
);
$this->set('reservations', $this->paginate($this->modelClass));
$this->set('searchTerm', $searchTerm);
$this->set('model', $this->modelClass);
}
我的表格:
<?php
echo $this->Form->create($model, array(
'action' => 'index'));
echo $this->Form->input('searchTerm', array(
'label' => false, 'style' => 'float:left;', 'class' => 'offset1'));
echo $this->Form->submit(__('Search'), array('class' => 'btn', 'style' => ''));
echo $this->Form->end();
?>
我在模型中的代码:
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
array('name' => 'searchTerm', 'type' => 'like', 'fields'=>array(/*'Reservation.start', 'Reservation.ucode', 'Reservation.ticket_nr', 'Location.name',*/ 'User.username'/*, 'Opmenu.name', 'Queue.name'*/)));
更新:
我将其包括在内以使其有效,但这可能不是最佳方式:
$conditions = array();
$conditions[] = array("Reservation.start LIKE '%{$searchTerm}%'");
$conditions[] = array("Reservation.ucode LIKE '%{$searchTerm}%'");
$conditions[] = array("Reservation.ticket_nr LIKE '%{$searchTerm}%'");
$conditions[] = array("Location.name LIKE '%{$searchTerm}%'");
$conditions[] = array("User.username LIKE '%{$searchTerm}%'");
$conditions[] = array("Opmenu.name LIKE '%{$searchTerm}%'");
$conditions[] = array("Queue.name LIKE '%{$searchTerm}%'");
答案 0 :(得分:0)
好的,所以在阅读了我的插件自述文件之后,我开始工作了。
这是我的模型代码:
public $actsAs = array('Search.Searchable');
public $filterArgs = array(
'searchTerm' => array(
'type' => 'like',
'field'=>array('Reservation.start', 'Reservation.ucode', 'Reservation.ticket_nr', 'Location.name', 'User.username', 'Opmenu.name', 'Queue.name')),
'filter' => array('type' => 'query', 'method' => 'orConditions'));
public function orConditions($data = array()) {
$filter = $data['filter'];
$cond = array(
'OR' => array(
'Reservation.start LIKE' => '%' . $filter . '%',
'Reservation.ucode LIKE' => '%' . $filter . '%',
'Reservation.ticket_nr LIKE' => '%' . $filter . '%',
'Location.name LIKE' => '%' . $filter . '%',
'User.username LIKE' => '%' . $filter . '%',
'Opmenu.name LIKE' => '%' . $filter . '%',
'Queue.name LIKE' => '%' . $filter . '%'
));
return $cond;
}
控制器:
public $components = array('Search.Prg');
public $presetVars = true;
public function index() {
$this->Prg->commonProcess();
$this->paginate = array();
$conditions = $this->Reservation->parseCriteria($this->passedArgs);
$searchTerm = '';
$this->set('reservation_mode', Configure::read('webakis.reservation_type'));
$this->Reservation->recursive = 0;
$this->paginate = array(
'fields' => array('Reservation.id', 'Reservation.start', 'Reservation.ucode', 'Reservation.ticket_nr', 'Location.name', 'User.username', 'User.id', 'Opmenu.name', 'Queue.name'),
'conditions' => array(
// 'ucode <>' => 0,
'user_id <>' => 0,
'deleted' => 0,
// 'start >=NOW() '
),
'order' => array(
'Reservation.start' => 'asc'
),
'limit' => 12,
);
$this->set('reservations', $this->paginate($this->modelClass, $conditions));
$this->set('searchTerm', $searchTerm);
$this->set('model', $this->modelClass);
}