0

我正在为搜索引擎制作 Zend 框架方法。因为我搜索不同的表,所以我还必须创建两个不同的选择变量并返回它。我的问题是:对于具有不同表格的搜索引擎来说,这是一个好主意吗?

以及如何在数组中返回不同的结果?( fetchall->$selectMovies, fetchall->$selectPosts)

谢谢和问候埃里克

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); 

    }
4

1 回答 1

0

您只能返回一次,但您可以做的是返回结果数组的数组。

<?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));
}
?>

或者将您的方法拆分为 2 并将一种搜索类型作为参数传递给主搜索方法:

<?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');
?>
于 2012-11-04T14:51:29.640 回答