1

In croogo's search-View from the Nodes-controller the results include only nodes, where the searched term was found on the Node's model fields, but will skip any translated content for the nodes.

I'm trying to override this functionality and add support for translated content as well, but having a hard time to override the search-view without having to override the whole node-controller.

Does anyone has done this before and could give me some advice, as which approach could I take?

4

1 回答 1

0

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

  • 在我的插件上添加一个SearchController带有show-view 的新插件,它是NodesController.
  • 用我的新视图覆盖search/*路线。

为了在翻译后的字段中进行搜索,我提供了两种可能的方法:

1)如果只为此使用分页,则需要先连接节点和 i18n 表,然后在连接的字段上另外查找字符串。

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

我最终选择了备选方案 2,这就是显示视图的样子:

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 ...
}
于 2013-04-19T12:28:35.660 回答