in my Book model.
/**
* Overridden paginate
*/
function paginate($conditions, $fields, $order, $limit, $page = 1,
$recursive = null, $extra = array()) {
if( $this->searchCategories){
$this->bindModel(array('hasOne' => array(
'BooksCategory' => array(
'className' => 'BooksCategory',
'foreignKey' => false,
'type' => 'inner',
'conditions' => array('BooksCategory.book_id = Book.id',
'BooksCategory.category_id' => $this->categories )
)
)), false );
}
if( $this->searchAuthors ){
$this->bindModel(array( 'hasOne' => array(
'BooksAuthor',
'FilterAuthor' => array(
'className' => 'Author',
'foreignKey' => false,
'type' => 'inner',
'conditions' => array('FilterAuthor.id = BooksAuthor.author_id')
))));
}
return $this->find('all', array(
'conditions' => $conditions,
'fields' => $fields,
'order' => $order,
'limit' => $limit,
'page' => $page,
'recursive' => $recursive));
}
/**
* Overridden paginateCount method
*/
function paginateCount($conditions = null, $recursive = 0, $extra =
array()) {
if( $this->searchCategories){
$this->bindModel(array('hasOne' => array(
'BooksCategory' => array(
'className' => 'BooksCategory',
'foreignKey' => false,
'type' => 'inner',
'conditions' => array('BooksCategory.book_id = Book.id',
'BooksCategory.category_id' => $this->categories )
))), false );
}
if( $this->searchAuthors ){
$this->bindModel(array( 'hasOne' => array(
'BooksAuthor',
'FilterAuthor' => array(
'className' => 'Author',
'foreignKey' => false,
'conditions' => array('FilterAuthor.id = BooksAuthor.author_id')
))));
}
$records = $this->find('all', array(
'conditions' => $conditions,
'fields' => array( 'DISTINCT Book.id' ),
'recursive' => $recursive));
return count($records);
}
On Tue, 2011-01-25 at 11:22 +0000, Steve wrote:
> Can anyone help with this ?
>
> Steve
>
> On Sun, 2011-01-23 at 15:22 +0000, Steve wrote:
> > 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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
No comments:
Post a Comment