I have a database with the following tables.

book -  Holds id, title, description, publisher ...
author - Holds id, name
books_author - Holds id, book_id, author_id

So a many-to-many relationship on the book and author as one book can
have many authors and one author can have many books.

To search for books with a text field, I have the following function in
the Book model

/**
 * Build conditions to search books for a string of text.
 * @param string $search
 * @return array of books that contain the searched for text in their
database definition.
*/
function searchConditions( $search ) {
  $like = '%' . $search . '%';
  $conditions = array (
    "OR" => array (
      "Book.isbn LIKE" => $like,
      "Book.ean LIKE" => $like, 
      "Book.name LIKE" => $like,
      "Book.description LIKE" => $like,
      "Book.publisher LIKE" => $like
    )
  );
  return $conditions;
}

My BookContoller class calls this in the following manner :

/**
  Display books to user optionally filtered by search text. The text
  will have been stored in $this->search by the search() function.
*/
function index() {
  $this->Book->recursive = 1;
  if($this->search) {
    $this->paginate = array(
      'limit' => 12,
      'order' => array('Book.created' => 'desc'),
      'conditions' => $this->Book->searchConditions($this->search));
  } else {
    $this->paginate = array(
      'limit' => 12,
      'order' => array('Book.created' => 'desc'));
  }
  $this->set('books', $this->paginate('Book'));
}


Now...

How do I adapt the search to include the names of the books authors ?



-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to