I am making a Zend framework method for a search engine. Because i search through different tables i also have to make two different select variables and return it. My questions are: Is this a good idea for a search engine with different tables?
And how to return the different resuls in an array? (fetchall->$selectMovies
, fetchall->$selectPosts
)
Thanks and greetz Eric
public function search($keyword) {
$selectPosts = $this->select();
$selectPosts->from('posts', array('*'));
$selectPosts->where('posts.post_title LIKE ? OR posts.post_body LIKE ?', '%'. $keyword . '%' );
$tblMovies = new Model_DbTable_Film();
$selectMovies = $tblMovies->select();
$selectMovies->from('film', array('*'));
$selectMovies->where('film.titel LIKE ? OR film.omschrijving LIKE ?', '%'. $keyword . '%' );
//var_dump($this->fetchAll($selectPosts, $selectMovies)); die;
return $this->fetchAll($selectMovies);
return $this->fetchAll($selectPosts);
}
You can only return once, but what you can do is return an array of your result arrays.
<?php
public function search($keyword) {
$selectPosts = $this->select();
$selectPosts->from('posts', array('*'));
$selectPosts->where('posts.post_title LIKE ? OR posts.post_body LIKE ?', '%'. $keyword . '%' );
$tblMovies = new Model_DbTable_Film();
$selectMovies = $tblMovies->select();
$selectMovies->from('film', array('*'));
$selectMovies->where('film.titel LIKE ? OR film.omschrijving LIKE ?', '%'. $keyword . '%' );
//var_dump($this->fetchAll($selectPosts, $selectMovies)); die;
return array('movies'=>$this->fetchAll($selectMovies),
'posts'=>$this->fetchAll($selectPosts));
}
?>
Or split up your method into 2 and pass a type of search as a parameter to the main search method:
<?php
public function search($keyword,$type) {
return $this->{$type}($keyword);
}
public function search_movies($keyword) {
$tblMovies = new Model_DbTable_Film();
$selectMovies = $tblMovies->select();
$selectMovies->from('film', array('*'));
$selectMovies->where('film.titel LIKE ? OR film.omschrijving LIKE ?', '%'. $keyword . '%' );
return $this->fetchAll($selectMovies);
}
public function search_posts($keyword) {
$selectPosts = $this->select();
$selectPosts->from('posts', array('*'));
$selectPosts->where('posts.post_title LIKE ? OR posts.post_body LIKE ?', '%'. $keyword . '%' );
return $this->fetchAll($selectPosts);
}
?>
<?php
$result['movies'] = $model->search($keyword,'search_movies');
$result['posts'] = $model->search($keyword,'search_posts');
?>