CakePHP is losing search parameters when using pagination SOLVED

Cake-logo

The problem

Imagine we have a custom form to search for purchases done between two dates; so we have a simple action in a controller that takes the start and end date and create a set of conditions for the paginate() method, but… for some reason once we click on next page we are loosing the conditions to make the search.

The solution

A simple solution for saving the search parameters is just to store them in a session variable; following next algorithm:

	// START: We check if the conditions for search already exist
	$this->conditions = array();
	if($this->Session->check('conditions')){
        $this->conditions = $this->Session->read('conditions');
	}
	// END: Check

    // Normal POST request  
	if ($this->request->is('post')) {
		$data = $this->request->data;

		// We get the search parameters
		$fromDate = $data['Purchase']['FromDate'];
		$toDate = $data['Purchase']['ToDate'];

        // We create our conditions (this syntax can be improved)
		$conditions = "Purchase.date_delivered >= " 
                        . "'" . $fromDate . "'" 
                        . " AND Purchase.date_delivered <= " . "'" . $toDate . "'";

        // Passing conditions to a property
		$this->conditions = array ($conditions);


        // START: Saving conditions in a session variable
		$this->Session->write('conditions',$this->conditions);
		// END: Saving
		
        // Doing the normal pagination stuff
        $this->paginate['conditions'] = $this->conditions;
        $purchases = $this->paginate();
        $this->set('purchases',$purchases);

	else { // Pagination request (GET)
		$this->Purchase->recursive = 0;
		$this->paginate['conditions'] = $this->conditions; // We use the conditions stored previously
		// Doing the normal pagination stuff
        $purchases = $this->paginate();
        $this->set('purchases',$purchases);
	}

And that’s it! Now you have your search parameters working with pagination.

Enjoy!

Añade un Comentario

4 comments on “CakePHP is losing search parameters when using pagination SOLVED”

  1. Hola, estoy comenzando a programar con cake Php y necesito poder buscar una archivo de extencion dbf y poder leer la informacion que esta en esta y guardar en una tabla mysql. Si pudieras ayudarme como debo hacerlo te lo agradeceria mucho.

    1. Que tal Maria buen día,

      Sinceramente no he trabajado con archivos de esa extensión, supongo que son de alguna especie de base de datos. En cualquier caso veo que son dos problemas diferentes:

      1) Abrir, explorar y extraer la información del archivo .dbf.
      2) Crear un script (no necesariamente con CakePHP, ya que pudiera hacerse con PHP “simple”) para insertar datos directamente a una tabla MySQL.

      ¿Has investigado si el formato .dbf es binario? Si es así ¿es un binario propietario o existe algún paquete en algún lenguaje que te ayude a abrirlo?

      Saludos.

Leave a Reply to alex-arriaga

Your email address will not be published. Required fields are marked *