0

I've tried every simple cakedc search setup, and followed code samples from previous posts on this but it just won't seem to cooperate for me. I'm not sure, but there doesn't seem to be any search query generated with the search string, judging from a query log I have running on my database.

When I try to search, from /books/search, I get no results on a title I know is in the database and the URL changes to /books/index/title:sweet (I'm not sure if that is relevant).

Any help would be much appreciated.

Model

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

public $hasMany = array(
    'Contributor' => array (
        'className' => 'Contributor',
        'foreignKey' => 'isbn13'
        ),
    'Override' => array (
        'className' => 'Override',
        'foreignKey' => 'isbn13'
        )

);

public $filterArgs = array(
        'title' => array('type' => 'query', 'method' => 'filterTitle'),
         'main_desc' => array('type' => 'value')
);

public function filterTitle($data, $field = null) {
        if (empty($data['title'])) {
            return array();
        }
        $titleField = '%' . $data['title'] . '%';
        return array(
            'OR' => array(
                $this->alias . '.title LIKE' => $titleField,
                ));
}

Controller

class BooksController extends AppController {
public $helpers = array ('Html', 'Form');
public $paginate = array();
public $components = array('Search.Prg');
public $presetVars = true; 

public function index() {
    //get books
    $this->set('books', $this->Book->find('all'));

    //search
   /* $this->Prg->commonProcess();
    $this->paginate['conditions'] = $this->Book->parseCriteria($this->Prg->parsedParams());
    $this->set('books');
   */
}

 public function search($books = NULL) {
    $this->Prg->commonProcess();
    $this->paginate['conditions'] = $this->Book->parseCriteria($this->passedArgs);
    $this->set('books', $this->paginate());
}

View

<h1>Search Results</h1> 
<?php
     echo $this->Form->create('Book'   , array(
        'url' => array_merge(array('action' => 'search'), $this->params['pass'])
        ));

    echo $this->Form->input('title', array('div' => false, 'empty' => true)); // empty       creates blank option.

    echo $this->Form->submit(__('Search', true), array('div' => false));
    echo $this->Form->end();

Routes

Router::connect('/', array('controller' => 'books', 'action' => 'index'));

Router::connect('/index/*', array('controller' => 'books', 'action' => 'search'
 )); 

Router::connect('/view/:url', array('controller' => 'books', 'action' => 'view', array('url' => '[\w\W]*')
 ));
Router::connect('/edit/:url', array('controller' => 'books', 'action' => 'edit', array('url' => '[\w\W]*')
 ));
Router::connect('/whatsnew/*', array('controller' => 'books', 'action' => 'whatsnew'
 ));
Router::connect('/search/*', array('controller' => 'books', 'action' => 'search'
 )); 

I've renamed the search-master folder to Search and put it in my plugins directory and In my bootstrap.php I've loaded the plugin

CakePlugin::load('Search');  
jnm
  • 1
  • 1
  • When I add `debug($this->paginate['conditions'] = $this->Book->parseCriteria($this->passedArgs));` to the search action, I get `array( 'Book.title LIKE' => '%sweet%' )` but I don't where/if this gets turned into a query – jnm Sep 25 '13 at 16:40
  • What is the resulting SQL query? Also: You should always mention the exact cakephp version you are using. – mark Sep 25 '13 at 17:44
  • Ok, I've got it down to the query. The problem is that conditions are not being submitted. But WHY? The query is as follows 'SELECT `Book`.`book_id`, `Book`.`isbn13`, `Book`.`audience`, `Book`.`age_from`, `Book`.`age_to`, `Book`.`grade_from`, `Book`.`grade_to`, `Book`.`main_desc`, `Book`.`bio_note`, `Book`.`review_quote`, `Book`.`imprint`, `Book`.`publisher`, `Book`.`pub_status`, `Book`.`pub_date`, `Book`.`height`, `Book`.`width`, `Book`.`url`, `Book`.`extras` FROM `titles`.`books` AS `Book` WHERE 1 = 1 LIMIT 20. – jnm Sep 25 '13 at 19:58
  • Thanks for the feedback @mark, I'm using cake 2.3.8 and CakeDC search 2.3. I also tried with the latest dereuromark search plugin based on cakeDC, but I have the same results: no conditions being generated in the query. – jnm Sep 26 '13 at 12:59
  • Are u using the development branch of the CakeDC one? Try that one. – mark Sep 26 '13 at 13:59
  • Thanks @mark, I just tried with the latest development branch and no luck.. same results. Now I'm trying to see where the conditions fail to pass forward, but having little luck getting models to tell me what's being held in their variables.. – jnm Sep 26 '13 at 15:19

1 Answers1

0

Fixed! Needed

$this->set('books', $this->paginate($this->Book->parseCriteria($this->passedArgs)));' 

instead of

$this->set('books', $this->paginate());'

in the controller. Not sure why though..

jnm
  • 1
  • 1