使用CakeDC搜索插件与关联模型 [英] Using the CakeDC search plugin with associated models

查看:128
本文介绍了使用CakeDC搜索插件与关联模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CakePHP 1.3.8,我已安装 CakeDC搜索插件 a>。我有一个教程模型,它是一个HABTM与LearningGoal模型的关系。

I'm using CakePHP 1.3.8, and I've installed the CakeDC Search plugin. I have a Tutorial model, which is in a HABTM relationship with a LearningGoal model.

我有一个搜索操作&视图,我可以成功地在教程模型中搜索字段。我也想过滤我的教程搜索结果使用在同一个表单上的学习目标复选框。我试过添加各种参数到教程的$ filterArgs和TutorialsController的$ presetVars。我也试过把相关的$ filterArgs移动到LearningGoal模型。我还没有能够成功触发学习目标在$ filterArgs的条目。

I have a search action & view in the Tutorials controller with which I can successfully search fields in the Tutorial model. I'd also like to filter my tutorial search results using LearningGoal checkboxes on the same form. I've tried adding various parameters to Tutorial's $filterArgs and TutorialsController's $presetVars. I've also tried moving the relevant $filterArgs to the LearningGoal model. I have not yet been able to successfully trigger the entry for learning goals in $filterArgs.

我想我必须缺少一些明显的东西。或者搜索插件不支持我想做的。有没有人知道如何使用这个插件来搜索相关的模型?

I think I must be missing something obvious. Or maybe the Search plugin doesn't support what I'm trying to do. Does anyone know how to use this plugin to search on associated models?

推荐答案

教程模型中的$ filterArgs部分必须如下所示:

The $filterArgs piece in the Tutorial model must look like this:

var $filterArgs = array(
  array('name' => 'LearningGoal', 'type' => 'subquery', 'method' => 'findByLearningGoals', 'field' => 'Tutorial.id'),
);

这是教程模型中的支持函数:

Here's the supporting function in the Tutorial model:

function findByLearningGoals($data = array()) {
  $ids = explode('|', $data['LearningGoal']);
  $ids = join(',', $ids);
  $this->LearningGoalsTutorial->Behaviors->attach('Containable', array('autoFields' => false));
  $this->LearningGoalsTutorial->Behaviors->attach('Search.Searchable');

  $query = $this->LearningGoalsTutorial->getQuery('all',
     array(
       'conditions' => array('LearningGoalsTutorial.learning_goal_id IN (' . $ids . ')'),
       'fields' => array('tutorial_id'),
     )
  );
  return $query;
}



在TutorialsController中,$ presetVars应如下所示:

In TutorialsController, $presetVars should look like this:

public $presetVars = array(
  array('field' => 'LearningGoal', 'type' => 'checkbox', 'model' => 'Tutorial'),
);

在我在TutorialsController中的搜索操作中,我做到了:

And in my search action in TutorialsController, I did this:

$this->LearningGoal = $this->Tutorial->LearningGoal;

Prg组件似乎需要。

The Prg component seems to need that.

这篇关于使用CakeDC搜索插件与关联模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆