나는
나는 항상 내가 시도와 자신을 상기시키기 위해 쓴 있도록 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
내가이 짓을 한 이유에 더 설명을 원하는 경우에, 질문하고 당신이 요구 한 저에게 이메일을받을 경우 내가 만약 내가 대답을 줄 것이다 할 수있다.