/**
  * Returns numbers of books by a genre
  *
  * @param int $num
  */
 public function countBookyByGenre($genre)
 {
     $query = 'SELECT count(*) as num FROM {%table%} as bg {%conditions%}';
     $args = $this->_prepareArgs(BookGenreModel::table(), array('bg.genre_id' => $genre));
     $query = $this->_queryTemplate($query, $args);
     $result = $this->query($query);
     $row = $this->_assocRows($result);
     return $row[0]['num'];
 }
 public function searchBooks($str, $cat = null, $page = 0, $size = 10)
 {
     $conditions = '(name LIKE "%{%str%}%" OR isbn LIKE "%{%str%}%") {%cond_genre%}';
     $query = 'SELECT {%columns%} FROM {%table%} as b' . '  LEFT JOIN {%table_book_genre%} as bg ON (b.id = bg.book_id)' . '  LEFT JOIN {%table_genre%} as g ON (bg.genre_key = g.id)' . 'WHERE {%conditions%} {%orders%} {%limit%}';
     $args = $this->_prepareArgs(BookModel::table(), null, null, 'name ASC', array($page, $size));
     $args['table_book_genre'] = BookGenreModel::table();
     $args['table_genre'] = GenreModel::table();
     $args['conditions'] = $conditions;
     $args['str'] = $this->_esc($str);
     $args['cond_genre'] = is_null($cat) ? '' : ' AND c.key = "' . $this->_esc($cat) . '" ';
     $query = $this->_queryTemplate($query, $args);
     die($query);
     $result = $this->query($query);
     return $this->_assocRows($result);
 }
    BookGenreModel::create(array('book_id' => 7, 'genre_id' => 10));
    BookGenreModel::create(array('book_id' => 8, 'genre_id' => 13));
    BookGenreModel::create(array('book_id' => 9, 'genre_id' => 12));
    BookGenreModel::create(array('book_id' => 10, 'genre_id' => 12));
    BookGenreModel::create(array('book_id' => 11, 'genre_id' => 12));
    BookGenreModel::create(array('book_id' => 12, 'genre_id' => 3));
    BookGenreModel::create(array('book_id' => 13, 'genre_id' => 4));
    BookGenreModel::create(array('book_id' => 14, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 15, 'genre_id' => 8));
    BookGenreModel::create(array('book_id' => 16, 'genre_id' => 9));
    BookGenreModel::create(array('book_id' => 17, 'genre_id' => 10));
    BookGenreModel::create(array('book_id' => 18, 'genre_id' => 11));
    BookGenreModel::create(array('book_id' => 19, 'genre_id' => 13));
    BookGenreModel::create(array('book_id' => 20, 'genre_id' => 12));
    BookGenreModel::create(array('book_id' => 21, 'genre_id' => 13));
    BookGenreModel::create(array('book_id' => 22, 'genre_id' => 3));
    BookGenreModel::create(array('book_id' => 23, 'genre_id' => 4));
    BookGenreModel::create(array('book_id' => 24, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 25, 'genre_id' => 8));
    BookGenreModel::create(array('book_id' => 26, 'genre_id' => 9));
    BookGenreModel::create(array('book_id' => 27, 'genre_id' => 10));
    BookGenreModel::create(array('book_id' => 28, 'genre_id' => 11));
    BookGenreModel::create(array('book_id' => 29, 'genre_id' => 12));
    BookGenreModel::create(array('book_id' => 30, 'genre_id' => 12));
}
if (!$db->tableExists('order')) {
    OrderModel::create(array('user_id' => 1, 'datetime' => "2014-11-11 11:11:11"));
}
if (!$db->tableExists('position')) {
    PositionModel::create(array('order_id' => 1, 'book_id' => 1, 'amount' => 1, 'price' => 12.5));
}
    BookGenreModel::create(array('book_id' => 9, 'genre_id' => 0));
    BookGenreModel::create(array('book_id' => 10, 'genre_id' => 0));
    BookGenreModel::create(array('book_id' => 10, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 11, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 12, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 13, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 14, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 15, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 16, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 18, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 19, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 20, 'genre_id' => 1));
    BookGenreModel::create(array('book_id' => 20, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 21, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 22, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 23, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 24, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 25, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 26, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 28, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 29, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 30, 'genre_id' => 2));
    BookGenreModel::create(array('book_id' => 31, 'genre_id' => 3));
    BookGenreModel::create(array('book_id' => 32, 'genre_id' => 3));
}
if (!$db->tableExists('order')) {
    OrderModel::create(array('user_id' => 1, 'datetime' => "2014-11-11 11:11:11"));
}
if (!$db->tableExists('position')) {
    PositionModel::create(array('order_id' => 1, 'book_id' => 1, 'amount' => 1, 'price' => 12.5));
}