0

我正在使用 cakephp 2.1 和 cakedc 搜索插件。
问题是我不能把它粘在一起。还得到:

Notice (8): Indirect modification of overloaded property ProjectsController::$paginate has no effect [APP\Controller\ProjectsController.php, line 48]

遵循 cakedc 教程,但缺少一些东西!简单的搜索不会过滤任何内容。
我想按名称字段过滤。

在我的项目模型上

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

在我的控制器中:

public $components = array('Search.Prg');

public $presetVars = array(
    array('field' => 'name', 'type' => 'value')
);

索引函数更新为仅使用 index.ctp(无查找函数)

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

并将搜索表单添加到 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();

我知道这一定是我的一些明显错误,请多多包涵。任何人都可以帮忙吗?

非常感谢 !

4

1 回答 1

10

我很高兴让人们知道我找到了使用 cakedc 搜索插件的方法。

首先,我必须密切关注 1.3 的本教程(起初我认为它不适用于 2.1,但它就像魅力一样。)

http://www.youtube.com/watch?v=FAVuLXFVaCw

并从 cakedc http://cakedc.com/eng/downloads/view/cakephp_search_plugin
下载 1.3 示例代码 以了解视频教程。(尝试按照自述文件的说明运行它,但出现错误 Fatal error: Class 'Dispatcher' not found in C:\wamp\www\search\webroot\index.php on line 83 所以选择只获取代码片段并让它们工作2.1)

回到我的项目:

1.- 从 cakedc https://github.com/CakeDC/search下载搜索版本 2.1 ,
文件 CakeDC-search-2.1-0-g834f79f.zip。

2.- 将所有文件放在 /plugins/Search/ 文件夹中

3.- 添加

CakePlugin::load('Search');  

到 /Config/bootstrap.php 的底部

4.- 在我的控制器上,声明组件和 presetVars(我正在使用一个名为 name 的字段)

public $components = array('Search.Prg');
public $presetVars = array(
    array('field' => 'name', 'type' => 'value'),
    array('field' => 'pr_status', 'type' => 'value'),
);

5.-并更新了我的索引功能:

public function index() {
    $this->Prg->commonProcess();
    $this->paginate = array(
        'conditions' => $this->Project->parseCriteria($this->passedArgs));
    $this->set('projects', $this->paginate());
}

6.-在我的模型上,添加

        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.- 最后,在 index.ctp 上创建搜索表单,就在 table 标记的正上方

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

即使我使用的是 2.1,我也学会了不要丢弃 1.3 的教程和文档。下一步是按日期和更好的搜索表单进行过滤。

祝大家好运。

卡洛斯

于 2012-05-16T16:40:32.450 回答