Monday, February 18, 2013

Re: Cake PHP Search Component not working and giving an error

Hi Euromark

I am using the search plugin on https://github.com/cakedc/search. I am trying to search for records using the ID field only, that is if i type in 1 on my form input it must show a record with user ID 1. The challenge that i am having now is when i specify that the input should be the id field, the input field for the search functionality disappears on my index view, strange thing is when i specify a different field name the input field shows.

Below is my code for my Model

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

    public $filterArgs = array(
        'id' => array('type' => 'like', 'field' => 'ItSupportRequest.id'),
    );

    public function findByTags($data = array()) {
        $this->Tagged->Behaviors->attach('Containable', array('autoFields' => false));
        $this->Tagged->Behaviors->attach('Search.Searchable');
        $query = $this->Tagged->getQuery('all', array(
            'conditions' => array('Tag.name'  => $data['tags']),
            'fields' => array('foreign_key'),
            'contain' => array('Tag')
        ));
        return $query;
    }

    public function orConditions($data = array()) {
        $filter = $data['filter'];
        $cond = array(
            'OR' => array(
                $this->alias . '.id LIKE' => '%' . $filter . '%',
            ));
        return $cond;
    }
   
and here is my controller code.

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

    public $presetVars = true; // using the model configuration

    public function find() {
        $this->Prg->commonProcess();
        $this->paginate['conditions'] = $this->ItSupportRequest->parseCriteria($this->passedArgs);
        $this->set('articles', $this->paginate());
    }
   
and in my index.ctp file i have this code.

<?php
        echo $this->Form->create('ItSupportRequest', array(
    'url' => array_merge(array('action' => 'find'), $this->params['pass'])
));
        echo $this->Form->label('Query ID:') . "<br/>";       
        echo $this->Form->input('name', array('div' => false, 'label' => false));
   
echo $this->Form->submit(__('Search'), array('div' => false));
echo $this->Form->end();
        ?>   
   
Thanks

Victor

On Mon, Feb 18, 2013 at 1:10 PM, euromark <dereuromark@gmail.com> wrote:
You would need to properly upgrade the component and make it work with your version of cake.

It is up to date, more sophisticated and does the job with less configuration



Am Montag, 18. Februar 2013 09:43:46 UTC+1 schrieb Victor Musvibe:
Hi Bakers

I implemented a search component from this url http://www.designaeon.com/cake-php-search-component/ but I have a few problems. At first Cake didn't recognize the component. After I renamed search.php in SearchComponent.php

I got another Error: Declaration of SearchComponent::initialize() should be compatible with Component::initialize(Controller $controller) [APP/Controller/Component/SearchComponent.php, line 40]

Below is my code for SearchComponent

    <?php
    class SearchComponent extends Component {
  
    var $controller = null;
 
    function initialize(&$controller)
    {
    $this->controller = $controller;
    }
  
    function getConditions(){
    $conditions = array();
    $data= empty($this->controller->params['named']) ? $this->controller->params['url'] :     $this->controller->params['named'] ;
    $this->controller->{$this->controller->modelClass}->schema();     
    foreach($data as $key=>$value){
    if(isset($this->controller->{$this->controller->modelClass}->_schema[$key]) && !empty($value)){
    switch($this->controller->{$this->controller->modelClass}->_schema[$key]['type']){
    case "string":
    $conditions[$this->controller->modelClass . "." .$key . " LIKE"] =     "%".trim($value)."%";                       
    break;
    case "integer":
    $conditions[$this->controller->modelClass . "." .$key] =  $value;
    break;
    case "date":
    if(isset($this->controller->params['named'][$key."_fromdate"])){
    $from = date("Y-m-d", strtotime( $this->controller->params['named'][$key."_fromdate"] ));
    $conditions[$this->controller->modelClass.".".$key." >="] = trim($from);
    }
    if(isset($this->controller->params['named'][$key."_todate"])){
    $to = date("Y-m-d", strtotime($this->params['named'][$key."_todate"]));
    $conditions[$this->controller->modelClass.".".$key." <="] = $to;
    }
    }
    }
    }
    return $conditions;
       
    }
   
    }
    ?>

and in my index view i am using this code

    <div class="itsuportrequests index">
    <?php echo $this->element('SearchForm/set_pagination_named');?>//set pagination named element
    <?php echo $this->element('SearchForm/form') ?>//search form element
    <h2><?php __('ItSupportRequests');?></h2>
 
    <table cellpadding="0" cellspacing="0">
    <tr>
    <th><?php echo $this->Paginator->sort('id');?></th>
 
    <th class="actions"><?php __('Actions');?></th>
    </tr>
    <?php
     $i = 0;
    foreach ($itSupportRequests as $itSupportRequest):
    if ($i++ % 2 == 0) {
    }
    ?>
    <tr>
    <td><?php echo $itSupportRequest['ItSupportRequest']['id']; ?>&nbsp;</td>
    <td class="actions">
 
    <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $itSupportRequest['ItSupportRequest']['id'])); ?>
    <?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $itSupportRequest['ItSupportRequest']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $itSupportRequest['ItSupportRequest']['id'])); ?>
    </td>
    </tr>
    <?php endforeach; ?>
    </table>
    <p>
    </p>
    </div>
   
I have created 2 Elements SearchForm/form.ctp and SearchForm/set_pagination_named.ctp

    /*form.ctp*/
    <div class='search' style="margin:10px;">
    <fieldset>
    <h2> Search <?php echo$this->params['controller'] ?></h2><hr/>
    <?php echo $this->Form->create(array('action' => 'index', 'type' => 'GET')); ?>
    <?php echo $this->Form->input('id', array( 'label'=>'ID','type'=>'text'));
    echo $this->Form->submit('Search',array('class'=>'button'));
    ?>
 
    <?php echo $this->Form->end(); ?>
    </fieldset>
    </div>
   
and here is my     set_pagination_named.ctp

    <?php
    $data_arr=array();
    if(isset($this->params['url']) || isset($this->params['named'])){
    $data=isset($this->params['named'] ) && !empty($this->params['named']) ? $this->params['named']:$this->params['url'];
    $possibledata=array('id');//possible fields
    foreach($data as $key=>$value){
    if(in_array($key,$possibledata) && !empty($value)){
    $data_arr[$key]=$data[$key];
    }
    }
    }
    ?>
    <?php
    $this->Paginator->options(array(
    'evalScripts' => true,
    'url'=>$data_arr
    ));
    ?>

Thank you in advance.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: