LoginSignup
1
1

More than 5 years have passed since last update.

CakePHP、PaginatorでfindTypeを使う

Last updated at Posted at 2015-07-21

System

CakePHP 2.x

Code

Model/Foo.php

<?php
App::uses('AppModel', 'Model');
/**
 * Foo Model
 *
 */
class Foo extends AppModel {
/**
 * @var array
 */
        public $findMethods = array(
                'customFindType' => true,
        );
/**
 * @param $state
 * @param $query
 * @param array $results
 * @return array
 */
        public function _findCustomFindType($state, $query, $results = array()) {
                if ($state === 'before') {
                        // Custom find type
                        return $query;
                }
                return $results;
        }

}

Controller/FoosController.php

<?php
App::uses('AppController', 'Controller');
/**
 * Foos Controller
 *
 * @property Foo $Foo
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class FoosController extends AppController {

/**
 * Components
 *
 * @var array
 */
        public $components = array('Paginator', 'Session');

/**
 * index method
 *
 * @param null $findType
 * @return void
 */
        public function index() {
                $this->Foo->recursive = 1;
                $this->Paginator->settings = array(
                        'Foo' => array(
                                'findType' => 'customFindType'
                        )
                );
                $foos = $this->Paginator->paginate('Foo');
                $this->set(compact('foos'));
        }
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1