CakePHP / Croogo:如何在翻译的节点中搜索

时间:2013-04-18 12:58:38

标签: cakephp croogo

在croogo的搜索 - 来自节点控制器的视图中,结果仅包括节点,其中搜索的术语在节点的模型字段中找到,但将跳过节点的任何翻译内容。

我正在尝试覆盖此功能并添加对已翻译内容的支持,但很难覆盖搜索视图而无需覆盖整个节点控制器。

之前有没有人这样做过,可以给我一些建议,我可以采取哪种方法?

1 个答案:

答案 0 :(得分:0)

对于任何有兴趣的人,我最终都会做以下事情:

  • 使用SearchController视图在我的插件上添加新的show,这是NodesController上搜索视图的改编版本。
  • 使用我的新视图覆盖search/*路线。

为了搜索翻译的字段,我提出了两种可能的方法:

1)如果仅使用paginate,那么首先需要加入节点和i18n表,然后在连接的字段上另外查找字符串。

2)或者,首先通过查询找到i18n表上具有匹配内容的所有不同节点。然后使用此节点列表添加“OR Node.id in (list_of_nodes)”条件

我最终得到了替代2,这就是show-view的样子:

public function show() {
    if (!isset($this->request->params['named']['q'])) {
        $this->redirect('/');
    }

    App::uses('Sanitize', 'Utility');
    $q = Sanitize::clean($this->request->params['named']['q']);
    $results = $this->Node->query(
        "SELECT DISTINCT(foreign_key) FROM `i18n` " .
        "WHERE content LIKE '%" . $q . "%';");
    $node_ids = array();
    foreach($results as $res) {
        $node_ids[] = $res['i18n']['foreign_key'];
    }
    $this->paginate['Node']['order'] = 'Node.created DESC';
    $this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
    $this->paginate['Node']['conditions'] = array(
        'Node.status' => 1,
        'AND' => array(
            array(
                'OR' => array(
                    'Node.title LIKE' => '%' . $q . '%',
                    'Node.excerpt LIKE' => '%' . $q . '%',
                    'Node.body LIKE' => '%' . $q . '%',
                    'Node.id' => $node_ids,
                ),
            ),
            array(
                'OR' => array(
                    'Node.visibility_roles' => '',
                    'Node.visibility_roles LIKE' => '%"' . $this->Croogo->roleId . '"%',
                ),
            ),
        ),
    );
    // some more stuff ...
}