Dc搜索框插件在cakephp中不起作用?

时间:2015-10-27 09:18:37

标签: php cakephp

我安装了Dc搜索插件。我在交易页面上应用此插件,它正常工作。  但在联系索引页面上它无法正常工作。两个页面上的差异是联系页面包含多个表格的关系,如公司,公司_联系,单位,税收等等。但在交易页面数据只来自交易表。除此之外,两者都没有区别。第二个是作为联系索引页面的模型我使用company.php而不是contact.php。

在我的index.php中

        <?php if(!empty($search_args['search_cname'])): ?>
        <strong><?php echo __('Company Name:'); ?> </strong><span><?php echo $search_args['search_cname']; ?></span>
        <?php endif; ?>
        <?php if(!empty($search_args['search_name'])): ?>
        <strong><?php echo __('Contact Person:'); ?> </strong><span><?php echo $search_args['search_name']; ?></span>
        <?php endif; ?>
        <?php if(!empty($search_args['search_status'])): ?>
        <strong><?php echo __('Status:'); ?> </strong><span><?php echo $search_args['search_status']; ?></span>
        <?php endif; ?>
        <?php if(!empty($search_args['search_uname'])): ?>
        <strong><?php echo __('User Name:'); ?> </strong><span><?php echo $search_args['search_uname']; ?></span>
        <?php endif; ?>
    </div>
    <?php endif; ?>
    <div class="filter-box" style="display:none">
        <?php echo $this->Form->create('company', array('url' => array_merge(array('controller'=>'contacts','action' => 'index'), $this->params['pass']),'class'=>'form-inline')); ?>
        <fieldset>
        <legend><?php echo __(Filters); ?></legend>
            <?php echo $this->Form->input('search_status',array('div'=>false,'class'=>'span2','options'=>array(4=>'Lead',5=>'Opportunity',6=>'Account'),'label'=>'Status ',)); ?>
            <?php echo $this->Form->input('search_cname',array('div'=>false,'class'=>'span2','label'=>'Company Name ','placeholder'=>'Company Name')); ?>
            <?php echo $this->Form->input('search_name',array('div'=>false,'class'=>'span2','label'=>'Contact Person ','placeholder'=>'Contact Person')); ?>
            <?php echo $this->Form->input('search_uname',array('div'=>false,'class'=>'span2','label'=>'User Name ','placeholder'=>'User Name')); ?>
            <?php echo $this->Form->submit('Filter',array('div'=>false,'class'=>'btn btn-info')); ?>
        </fieldset>
        <?php echo $this->Form->end(); ?>
    </div>

在ContactController.php中

 $searched = false;
if ($this->passedArgs) {
        $args = $this->passedArgs;
        if(isset($args['search_cname'])){
            $searched = true;
        }
        if(isset($args['search_name'])){
            $searched = true;
        }
    }
    $this->set('searched',$searched);

在模型contact.php中

  public $actsAs = array(
        'Search.Searchable',
        );

            public $filterArgs = array(
                    'search_status' => array('type'=>'value','field'=>'company.contact_status_id'),
                    'search_cname' => array('type'=>'like','field'=>array('company.company_name')),
                    'search_name' => array('type'=>'like','field'=>array('company_contacts.first_name','company_contacts.last_name')),
                    'search_uname' => array('type'=>'like','field'=>'User.full_name'),

        'search_all' => array('type'=>'query','method'=>'searchDefault')
);

public function searchDefault($data = array()) {
    $filter = $data['search_all'];
    $cond = array(
            'OR' => array(
               // 'company.company_name LIKE' => '%' . $filter . '%',
                'company_contacts.first_name LIKE' => '%' . $filter . '%',

                'company_contacts.last_name LIKE' => '%' . $filter . '%',

                 'company_contacts.email LIKE' => '%' . $filter . '%',
                 'company_contacts.mobile_number LIKE' => '%' . $filter . '%',

                    $this->alias . '.company_name LIKE' => '%' . $filter . '%',
                        'User.full_name LIKE' => '%' . $filter . '%',
                //  $this->alias . '.first_name LIKE' => '%' . $filter . '%',
                //  $this->alias . '.last_name LIKE' => '%' . $filter . '%',
            //      $this->alias . '.status LIKE' => '%' . $filter . '%'
            ));
    return $cond;
}

2 个答案:

答案 0 :(得分:0)

首先在控制器中包含Prg组件,如下所示

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

然后在您的操作中,在顶部添加以下行

$this->Prg->commonProcess();

$this->Paginator->settings['conditions'] = $this->Contact->parseCriteria($this->passedArgs);
$this->set('searched', $this->Paginator->paginate());

有关详细信息,请查看here

答案 1 :(得分:0)

这是另一种方法,我自己的自定义

在控制器中添加以下行

public $presetVars = array();

然后

public function index() {
    $this->Prg->commonProcess();
    $this->Contact->data["Contact"] = $this->passedArgs;
    $this->request->data["Contact"] = $this->passedArgs;
    $parsedConditions[] = $this->filter_condition($this->passedArgs);

    $this->paginate = array(
        "conditions" => $parsedConditions
    );
    $this->set('result', $this->paginate());
}

public function filter_condition($data = array()) {
    $parsedConditions = array();
    if (!empty($data)) {
        $data = array_map('trim', $data);
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                foreach ($this-> Contact->filterArgs as $k => $v) {
                    if ($key == $v["name"]) {
                        $value = preg_replace('/\s+/', ' ', $value);
                        $value = Sanitize::clean($value);
                        if ($value != null) {
                            if ($v["type"] == "string") {
                                $parsedConditions[] = array("Contact.$key LIKE" => "%" . $value . "%");
                            } else if ($v["type"] == "value") {
                                $parsedConditions[] = array("Contact.$key" => $value);
                            }
                        }
                    }
                }
            }
        }
    }
    return $parsedConditions;
}

在模型中写下像这样的过滤器

/ *过滤文件以搜索* /

public $filterArgs = array(
        array("name" => "field1", "type" => "value"),
        array("name" => "field2", "type" => "string")
 );