2017-10-24 2 views
0

제목이 말하는 것을 수행해야합니다.CakePHP의 각 행에 대한 개수가있는 데이터 검색

표시해야하는 특정 쿼리가 있습니다. 원시 쿼리을하고 싶지 않습니다. 내가 만든 다른 쿼리처럼하고 싶습니다. 물론

Click here

내가 표시해야 내 데이터베이스 쿼리의 결과입니다

내가 내 인덱스에 표시해야하는 결과입니다. 내 사이트에서 동일한 것을 표현해야합니다.

다음 방법은 내가 만든 것입니다.

public function index() 
{ 
    $country= $this -> Auth -> User()['country_fk']; 
    $this->paginate['contain'] = ['Dogs','Dogs.Human','Dogs.Human.Cities','Users']; 
    $this->paginate['conditions'] = ['Users.isHuman' => 0, 'Cities.country_fk' => $country]; 
    $this->paginate['order'] = array('Badges.id' => 'desc'); 
    $badges= $this->paginate($this->Badges); 
    $this->set(compact('badges')); 
    $this->set('_serialize', ['badges']); 
} 

몇 가지 예를 들어 쿼리를 수행 한 후 계산을하지만 난 생각하지 것입니다 :하지만 계산의 부분을 수행하는 방법을 모른다 (내가 쿼리를 완료하는 데 필요한 유일한 부분입니다) 내가 찾고 있어요.

고마워요.

편집 arilia의 대답 후 는, 내 컨트롤러에서 나는 다음과 같은 라인을 가지고 :

<thead> 
     <tr> 
      <th><?= $this->Paginator->sort('idBadge', 'ID') ?></th> 
      <th><?= $this->Paginator->sort('name', 'Name') ?></th> 
      <th><?= $this->Paginator->sort('value1', 'Value 1') ?></th> 
      <th><?= $this->Paginator->sort('value2', 'Value 2') ?></th> 
      <th><?= $this->Paginator->sort('count', 'Quantity') ?></th> 
     </tr> 
    </thead> 
    <tbody> 
    <?php foreach ($badges as $badge): ?> 
     <tr> 
      <td><?= h($badge->idBadge) ?></td> 
      <td><?= h($badge->name) ?></td> 
      <td><?= h($badge->value1) ?></td> 
      <td><?= h($badge->value2) ?></td> 
      <td><?= h($badge->count) ?></td> 
     </tr> 
    <?php endforeach; ?> 
+0

이 cake2 확실히 당신에게있는 쿼리 빌더이 방법을 사용? 내가 아는 한 cake2는 페이지 매김 호출 후에 객체를 반환하지 않습니다 ... – arilia

+0

죄송합니다, 방금 수정했습니다. 나는 3.0이라고 생각한다. 그것은 오래된 프로젝트이고 케이크의 버전을 잘 기억하지 못했습니다. –

답변

0

먼저 당신이를 만들어야합니다

public function index() 
{ 
    $country= $this -> Auth -> User()['country_fk']; 
    $this->Turnos->virtualFields = array('count' => 'COUNT(*)'); 
    $this->paginate['contain'] = ['Dogs','Dogs.Human','Dogs.Human.Cities','Users']; 
    $this->paginate['conditions'] = ['Users.isHuman' => 0, 'Cities.country_fk' => $country]; 

    $this->paginate['group'] = array('Badges.value2'); 
    $this->paginate['order'] = array('Badges.value2' => 'desc'); 

    $badges= $this->paginate($this->Badges); 
    $this->set(compact('badges')); 
    $this->set('_serialize', ['badges']); 
} 

이것은 내가 값을 보여 위해 할 것입니다 가상 필드

$this->Badges->virtualFields['count'] = 'COUNT(*)'; 

그런 다음 그룹

$this->paginate['group'] = array('the field you want to group by'); 

에 있습니다하지만 당신은 쿼리에

$this->paginate['fields'] = array('count', ...); 

편집 해당 필드를 원하는 매기기 구성 요소를 말해야 : 위의 코드가 들어 cake2

입니다 케이크 3

필드와 그룹을 페이징 제 배열

에 추가하기 만하면됩니다.
$this->paginate['fields'] = 
[ 
    'count' => 'COUNT(*)' 
    // other fields you want to retrieve go here 
]; 

$this->paginate['group'] = ['the field you want to group by']; 

어쨌든 나는

$badges = $this->Badges->find() 
    ->select(['count' => 'COUNT(*)']) 
    ->select($this->Badges) // this command tells the query builder to  
          // select all the fields of badges table 
          // other than count 
    ->select($this->Badges->Dogs) 
    ->select($this->Badges->Dogs->Humans) 
    ->select(... and so on for every model you want ...) 
    ->contain(['Dogs','Dogs.Human','Dogs.Human.Cities','Users']) 
    ->where(['Users.isHuman' => 0, 'Cities.country_fk' => $country]) 
    ->group(['the field to group']) 
    ->order(['Badges.id' => 'desc']); 

$badges= $this->paginate(badges); 
+0

쿼리가 작동하지만 결과의 각 값에 iiterate를 사용하면 "count"열의 값이 비어집니다. 나는이 카운트가 각 뱃지가 아닌 일반적인 카운트를 제공한다고 생각합니다. 편집 질문을 참조하십시오 –

+0

또는 일부 부분에서 virtualFields 줄을해야 할 수도 있습니다, 나는 $ this-> paginate [ 'conditions'] –

+0

필드 목록에 가상 필드를 넣어야합니다. 내 adid를 참조하십시오 – arilia

관련 문제