2014-10-15 6 views
1

페이지 매김과 함께 사용되는이 사용자 지정 찾기 메서드가 있습니다. 그러나 생성하는 쿼리가 잘못되었습니다. 어떤 이유로 그것은 아무 이유없이 SELECT에 필드를 넣고 있습니다.CakePHP는 사용자 지정 페이지 매김을 할 때 잘못된 쿼리를 생성합니다.

$query['fields'] = array('COUNT(*)'); 

내가 gettign있어 오류가 이것이다 :

Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous

하지만 케이크는 명백한 이유를 위해이 쿼리를 생성 난 찾기 방법의 카운트 부분을 지정하지

SELECT COUNT(*), Country.id, State.id, ClientStatus.id, SalesStatus.id, id FROM...

내가 지금까지 발견 한 것은 배열을 포함하는 CompanyType을 제거하면 쿼리가 작동한다는 것입니다. CompanyType은 HABTM 연관이므로 데이터를 반환하려는 조건에는 적용하지 않습니다. Cake는 별도의 쿼리를 생성해야합니다. 그러나 어떤 접두사도없이 id이 쿼리에 포함되는 방식을 이해하지 못합니다. :(

내가에만 카운트 세트, 다른 분야. 왜 케이크 내가이 문제를 해결하려면 어떻게해야합니까? 여기에 특히 외로운 id을 다른 필드를 추가를 참조 $query 변수를 디버깅 할 때?

찾기 방법 :.

public function _findSearch($state, $query, $results = array()) { 
    $this->Behaviors->unload('Tags.Taggable'); 
    $this->unbindModel(array(
     'hasAndBelongsToMany' => array(
      'Tag', 
      //'CompanyType' 
     ) 
    ), false); 

    if ($state === 'before') { 
     $query['contain'] = array(
      'Country', 
      'State', 
      'SalesStatus', 
      'ClientStatus', 
      'CompanyType', 
     ); 
     $query['fields'] = array(
      $this->alias . '.id', 
      $this->alias . '.company', 
      $this->alias . '.company2', 
      $this->alias . '.company3', 
      $this->alias . '.street', 
      $this->alias . '.postal_code', 
      $this->alias . '.city', 
      $this->alias . '.selection_ranking', 
      $this->alias . '.role', 
      'Country.id', 
      'Country.country', 
      'State.id', 
      'State.name', 
      'ClientStatus.id', 
      'ClientStatus.name', 
      'SalesStatus.id', 
      'SalesStatus.name' 
     ); 
     if (isset($query['operation']) && $query['operation'] === 'count') { 
      $query['fields'] = array('COUNT(*)'); 
            //debug($query); 
     } 
     return $query; 
    } 
    if (($state === 'after') && isset($query['operation']) && $query['operation'] === 'count') { 
     return $results[0][0]['COUNT(*)']; 
    } 
    return $results; 
} 

답변

2

시도 하역과 거짓 autoFields =로 함유 성 행동을 다시로드

$this->Behaviors->unload('Containable'); 
$this->Behaviors->load('Containable', array(
    'autoFields' => false 
)); 

없음 COUNT (*) 이외의 필드는 선언되거나 필요하지 않지만 containable은 필드를 추가합니다. 지금까지 아무런 문제가 없습니다. 문제는 접두어없이 "id"를 추가하여 SQL 오류가 발생한다는 것입니다.

autoFields: (boolean, optional) auto-add needed fields to fetch requested bindings. DEFAULTS TO: true

해결 방법은 거짓 autoFields와 동작을 다시로드하는 것입니다 그것에서

는 문서입니다. 이것은 핵심 버그 인 것 같습니다 ... : (