Bonjour,
Cela fait 2 jours que je tourne en rond sur un problème de pagination de recherche. La solution doit surement être simple mais je n'arrive pas à trouver la réponse sur internet.
-> Je récupère bien correctement mot clé de mon formulaire de recherche dans $motcle
class DossiersController extends AppController {
public $uses = array('Dossier', 'Localisation' , 'Personne');
public function recherche() {
//récupération du mot clé
$motcle = $this->data'Dossier']'motcle'];
//changement du mot clé en Majuscule
$motcle = strtoupper($motcle);
//recherche des dossiers en fonction de la conrrespondance entre l'id et le mot clé
$dossiers = $this->Dossier->findById($motcle);
//recherche des personnes en fonction de la correspondance entre le nom ou le prenom et le motcle
$Findpersonnes = $this->Personne->find('all', array('conditions' => array( 'OR' => array('Personne.per_nom' => $motcle , 'Personne.per_prenom' => $motcle))));
//si il y a une réponse dans $dossiers , alors affiche la vue du dossier
if(!empty($dossiers)){
$this->Session->setFlash("Résultat pour '".$motcle."'", 'default', array('class'=>'success'));
return $this->redirect('/dossiers/view/'.$motcle) ;
}
//sinon si il y a une réponse dans $Findpersonnes,
//alors ça en envoi Findpersonne à la page dossiers/recherche.ctp
elseif(!empty($Findpersonnes)){
$this->Session->setFlash("Résultat pour '".$motcle."'", 'default', array('class'=>'success'));
$this->set('Findpersonnes', $Findpersonnes, $this->paginate());
$this->Personne->recursive = 0;
}
//sinon, affichage d'un message d'érreur en restant sur la page actuelle
else{
$this->Session->setFlash(__("Aucun résultat pour '".$motcle."'"));
return $this->redirect($this->referer());
}
}
voici ma vue dossiers/recherche.ctp
<div class="personnes index">
<h2><?php echo __('Personnes'); ?></h2>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('<b>{:count}</b> Personnes trouvées')
));
?> </p>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('dossier_id', 'Dossier N°'); ?></th>
<th><?php echo $this->Paginator->sort('PER_Nom','Nom'); ?></th>
<th><?php echo $this->Paginator->sort('PER_Prenom', 'Prenom'); ?></th>
<th><?php echo $this->Paginator->sort('PER_DateNaiss', 'Date de Naissance'); ?></th>
<th><?php echo $this->Paginator->sort('PER_LieuNaiss', 'Lieu Naissance'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php
foreach ($Findpersonnes as $personne): ?>
<tr>
<td><?php echo h($personne'Personne']'id']); ?> </td>
<td>
<?php echo $this->Html->link($personne'Dossier']'id'], array('controller' => 'dossiers', 'action' => 'view', $personne'Dossier']'id'])); ?>
</td>
<td><?php echo h($personne'Personne']'PER_Nom']); ?> </td>
<td><?php echo h($personne'Personne']'PER_Prenom']); ?> </td>
<td><?php echo h($personne'Personne']'PER_DateNaiss']); ?> </td>
<td><?php echo h($personne'Personne']'PER_LieuNaiss']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('Voir'), array('controller'=> 'personnes', 'action' => 'view', $personne'Personne']'id'])); ?>
<?php echo $this->Html->link(__('Editer'), array('controller'=> 'personnes', 'action' => 'edit', $personne'Personne']'id'])); ?>
<?php echo $this->Form->postLink( __('Supprimer'), array('controller'=> 'personnes', 'action' => 'delete', $personne'Personne']'id']), null,__ ('Voulez-vous vraiment supprimer ?', $personne'Personne']'id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<div class="paging">
<?php
echo $this->Paginator->first('<< ' . __('Première'), array(), null, array('class' => 'first disabled'));
echo $this->Paginator->prev('< ' . __('Précédente'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('Suivante') . ' >', array(), null, array('class' => 'next disabled'));
echo $this->Paginator->last(__('Dernière') . ' >>', array(), null, array('class' => 'last disabled'));
?>
</div>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('Nouvelle Personne'), array('action' => 'add')); ?></li>
<li><?php echo $this->Html->link(__('Lister les Dossiers'), array('controller' => 'dossiers', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('Nouveau Dossier'), array('controller' => 'dossiers', 'action' => 'add')); ?> </li>
</ul>
</div>
Donc quand je j'éffectue une recherche, j'ai bien les bons résultats qui s'affichent correctement avec une pagination qui semble correct à première vue.
mais quand on se rapproche un peu plus, on se rend compte que le compteur n'est pas du tout bon :
echo $this->Paginator->counter(array(
'format' => __('<b>{:count}</b> Personnes trouvées')
));
et m'affiche en réalité le nombre total de dossiers au lieu du nombre de résultat de ma recherche et la pagination m'indique que je peux accéder à plusieurs pages alors qu'en réalité, il ne devrait y avoir qu'une ou deux page (selon la recherche). et bien sur, quand je clic sur la page 2 (par exemple) il m'affiche un message d'erreur :
"La page n'est pas redirigée correctement".
Comment faire pour avoir une pagination qui corresponde à ma recherche ?