2011-05-13 4 views
0

저는 CakePHP 1.3.8을 사용 중이며 CakeDC Search plugin을 설치했습니다. LearningGoal 모델과의 HABTM 관계에있는 자습서 모델이 있습니다.관련 모델과 함께 CakeDC 검색 플러그인 사용

자습서 모델에서 필드를 성공적으로 검색 할 수있는 자습서 컨트롤러에서 검색 동작 &보기가 있습니다. 또한 동일한 양식의 LearningGoal 체크 박스를 사용하여 자습서 검색 결과를 필터링하고 싶습니다. 나는 Tutorial의 $ filterArgs와 TutorialsController의 $ presetVars에 다양한 매개 변수를 추가하려고 시도했습니다. 나는 또한 관련 $ filterArgs를 LearningGoal 모델로 옮겨 보았습니다. 아직 $ filterArgs에서 목표를 학습하기위한 항목을 성공적으로 트리거 할 수 없습니다.

나는 분명히 뭔가 빠져 있어야한다고 생각합니다. 또는 검색 플러그인이 내가하려는 일을 지원하지 않을 수도 있습니다. 누구든지이 플러그인을 사용하여 연결된 모델을 검색하는 방법을 알고 있습니까?

답변

0

그래서 여기에 제가 알아 낸 것이 있습니다. 아래의 내용을 검색 플러그인 지침과 결합하여 관련 모델을 검색 할 수 있습니다.

같이해야 튜토리얼 모델의 $ filterArgs 조각 :

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

자습서 모델에서 지원하는 기능입니다 : TutorialsController에서

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

, presetVars는 다음과 같이한다 $ :

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

TutorialsController의 검색 작업에서 다음을 수행했습니다.

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

Prg 구성 요소가 필요로하는 것 같습니다.

0

나는

나는 항상 내가 시도와 자신을 상기시키기 위해 쓴 있도록 CakeDC 검색 동작을 사용하여 작업을 수행하는 방법을 알아내는 시간을 보낼 프로젝트에서이 작업을 수행하기 위해 올 때마다 CakePHP의 버전 2.x를 사용하고 간단한 언어 내가해야 할 일. 나는 또한 마이클이 일반적으로 정확하더라도 자신의 프로젝트에 그것을 변경하는 것을 더 어렵게 만드는 설명이 없다는 것을 알아 차렸다.

"가지고 있고 많은 것"관계가 있고 결합 테이블, 즉 여러 개의 테이블을 함께 묶는 두 개의 필드가있는 테이블을 검색하려는 경우 관계에있는 테이블 중 하나의 ID 목록을 사용하여 하위 쿼리를 만들려는 많은 관계. 관계의 다른쪽에있는 테이블의 ID가 해당 레코드에 있는지 점검하고, 일치하는 경우 주 테이블의 레코드가 선택됩니다. 그들은 다음 Handover.id 선택되는 레코드의 핸드 오버 결정에 사용되는 (3)의 aro_id있는 경우

SELECT Handover.id, Handover.title, Handover.description 
FROM handovers AS Handover 
WHERE Handover.id in 
(SELECT ArosHandover.handover_id 
FROM aros_handovers AS ArosHandover 
WHERE ArosHandover.aro_id IN (3) AND ArosHandover.deleted != '1') 
LIMIT 20 

모든 레코드 ArosHandover로부터이 다음 예에서

선택한다.

CakeDC 검색 동작을 수행하는 방법.

첫째, 탐색 양식에 필드를 배치 : 등

echo $this->Form->create('Handover', array('class' => 'form-horizontal'));?> 
echo $this->Form->input('aro_id', array('options' => $roles, 'multiple' => true, 'label' => __('For', true), 'div' => false, true)); 

... 나는 ArosHandover 데이터 공간에서 양식 요소를 배치하지 않은

통지; 이것을 말하기위한 또 다른 방법은 양식 요청이 전송 될 때 aro_id 필드가 Handover라는 배열 아래에 배치된다는 것입니다. 유형이 '하위 쿼리가'난 당신이 적절한 기록을 찾을 수있게하기 위해에 의해 하위 쿼리를 만들 필요가 위에서 언급 한 바와 같이입니다

'aro_id' => array('name' => 'aro_id', 'type' => 'subquery', 'method' => 'findByAros', 'field' => 'Handover.id') 

통지 것을 : 변수 $ filterArgs 아래 모델에서

타입을 서브 쿼리로 설정하면 CakeDC가 SQL의 서브 쿼리 스 니펫을 생성하도록 명령합니다. 메소드는 코드를 작성할 함수 이름입니다. 필드 요소는 위의 예 쿼리의이 부분에 나타날 것입니다 필드의 이름입니다

다음
WHERE Handover.id in 

당신은 하위 쿼리 반환 함수를 쓰기 다음, 핸드 오버에서

function findByAros($data = array()) 
    { 
    $ids = ''; //you need to make a comma separated list of the aro_ids that are going to be checked 
    foreach($data['aro_id'] as $k => $v) 
    { 
     $ids .= $v . ', '; 
    } 
    if($ids != '') 
    { 
     $ids = rtrim($ids, ', '); 
    } 
    //you only need to have these two lines in if you have not already attached the behaviours in the ArosHandover model file 
    $this->ArosHandover->Behaviors->attach('Containable', array('autoFields' => false)); 
    $this->ArosHandover->Behaviors->attach('Search.Searchable'); 

    $query = $this->ArosHandover->getQuery('all', 
     array(
     'conditions' => array('ArosHandover.aro_id IN (' . $ids . ')'), 
     'fields' => array('handover_id'), //the other field that you need to check against, it's the other side of the many-to-many relationship 
     'contain' => false //place this in if you just want to have the ArosHandover table data included 
     ) 
    ); 
    return $query; 
    } 

을 컨트롤러 :

public $components = array('Search.Prg', 'Paginator'); //you can also place this into AppController 
public $presetVars = true; //using $filterArgs in the model configuration 
public $paginate = array(); //declare this so that you can change it 

// this is the snippet of the search form processing 
public function admin_find() 
    { 
    $this->set('title_for_layout','Find handovers'); 
    $this->Prg->commonProcess(); 
    if(isset($this->passedArgs) && !empty($this->passedArgs)) 
    {//the following line passes the conditions into the Paginator component 
     $this->Paginator->settings = array('conditions' => $this->Handover->parseCriteria($this->passedArgs)); 
     $handovers = $this->Paginator->paginate(); // this gets the data 
     $this->set('handovers', $handovers); // this passes it to the template 

내가이 짓을 한 이유에 더 설명을 원하는 경우에, 질문하고 당신이 요구 한 저에게 이메일을받을 경우 내가 만약 내가 대답을 줄 것이다 할 수있다.

관련 문제