Monday, February 18, 2013

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

Lets not try to duplicate everything and stay on
http://stackoverflow.com/questions/14936595/search-functionality-not-working-using-id-field-input

The main question still stands: If you use "name" as form field, why are you declaring they key in your behavior as "id"? it should then also be "name" to match.
You may answer at SO.



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.
 
 

No comments: