Like many others I needed paginate with a search form.  Instead of
using get and passing the url parameters around I used post for search
form. I know that takes away the user bookmark ability, but oh well.

This solution involves storing $this->data into the session when the
form is submitted and then retieving it upon subsquent requests.
Whether submitted or retrieved from the session $this->data is used to
build a conditions array that configures the paginate query.  This is
NOT a new idea, but a more concise example of how to do it.  I would
have added this to an older post on the subject, but I guess there is
time limit for replying to a post.  Please correct me if this a bad
solution.

Note: The custom paginate and paginateCount model methods are not
shown.

class CandidatesController extends AppController {
        var $name = 'Candidates';
        var $helpers = array('Html', 'Form', 'Javascript', 'Session');
        var $matrix_search_data_key = 'candidate.matrix.search.data';
        var $paginate = array();

        function matrix() {
        //check for page parameter (used to determine if this is a
fresh visit to matrix action)
        $position = strpos($this->params['url']['url'], 'page');
        $pageParameterFound = ($position != null) && ($position >= 0);

        //only get the search data from the session if the page
paramter is found,
        //if page parameter not found delete the search data session
variable in case it still exists
        if ($pageParameterFound && empty($this->data)) {
            //set the $this->data to the data array stored in the
session
            $this->data = $this->Session->read($this-
>matrix_search_data_key);
        } else {
            $this->Session->del($this->matrix_search_data_key);
        }

        $conditions = null;
        if (!empty($this->data)) {
            //if data is found use it to create the conditions array
            $desiredRating = $this->data['Candidate']
['desiredRating'];
            $desiredCandidateingTypes =  $this->data['CandidateType']
['CandidateType'];

            $conditions = array(
 
'candidates_candidateing_types.candidateing_type_id' =>
$desiredCandidateingTypes,
                                '1=1 GROUP BY Candidate.id HAVING
avg_overall_rating >= '.$desiredRating
                            );
            //save the data array to the session (to be retrieved when
page or column links are clicked)
            $this->Session->write($this->matrix_search_data_key, $this-
>data);
        }

        if (empty($conditions)){
            //no conditions found, use default conditions
            $conditions = array(
                                '1=1 GROUP BY Candidate.id'
                            );
        }

        //create paginate array that controls the paginate query
        $this->paginate = array(
                'limit' => 10, //using 1 for testing purposes
            'conditions' => $conditions
            );

        $this->Candidate->recursive = 1;
        $this->set('candidates', $this->paginate());

        //select candidate types to be used in select box
        $candidateTypes = $this->Candidate->CandidateType->find
('list');
                $this->set(compact('candidateTypes'));
        }
        //... other action methods ...
}//end of Candidate controller

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to