info:faire_un_moteur_de_recherche_avec_cakephp

Action disabled: source

Moteur de recherche cakephp

Il y a peut-être plus simple (étonnant qu'on puisse pas faire avec bake… peut-être mal cherché)

http://bakery.cakephp.org/articles/view/pagination to try

oui, trouvé un bon tutoriel, suivre plutôt que mes notes - nettement plus rapide

http://bakery.cakephp.org/articles/view/paginating-with-fulltext-searches A TESTER!!!

http://bakery.cakephp.org/articles/view/search-feature-to-cakephp-blog-example bug

http://code.google.com/p/searchable-behaviour-for-cakephp/ bug

http://bakery.cakephp.org/articles/view/search-feature-as-an-sql-query-in-2-steps

la suite marche et est assez rapide à mettre en place:

dans le model: rien à faire

dans le controller:

ajouter avant la fin du chevron fermant } de la class:

	   function search() {
	$cherche=$this->data['Adminuser']['q'];
$this->set('results',$this->Adminuser->find('all',array('conditions'=>array('Adminuser.Login LIKE'=>"%" .$cherche."%"))));
	   }

on peut aussi mettre du SQL directement:

   function search() {
	$cherche=$this->data['Truc']['q'];
#$this->set('results',$this->Truc->find('all',array('conditions'=>array('Truc.* LIKE'=>"%" .$cherche."%"))));
$this->set('results',$this->Truc->query("
SELECT * FROM truc WHERE 
`login` LIKE '%" .$cherche."%'
OR `User` LIKE '%" .$cherche."%'  
OR `User2` LIKE '%" .$cherche."%'  
OR `User3` LIKE '%" .$cherche."%'  
OR `Db` LIKE '%" .$cherche."%'  
OR `remarques` LIKE '%" .$cherche."%'  
OR `Type` LIKE '%" .$cherche."%'  
OR `version` LIKE '%" .$cherche."%'  
OR `email` LIKE '%" .$cherche."%'  
OR `homedir` LIKE '%" .$cherche."%'  
OR `website` LIKE '%" .$cherche."%'  
;"));
 
    } 

Dans le répertoire “views”, ajouter un fichier search.ctp:

adapter les noms des champs!

FIXME Attention à l'astuce liée à la structure de nom cake: si vous avez créé p.ex.

   <?php
   echo $form->create("Patchadmin",array('action' => 'search'));
    echo $form->input("q", array('label' => 'Search for'));
    echo $form->end("Search"); 
    ?>

il faudra impérativement mettre dans la vue:

			<?php echo $post['patchadmins']['type']; ?>

et surtout pas

			<?php echo $post['Patchadmin']['type']; ?>
        <?php $html->addCrumb('Truc', '/'); ?>
<h1>Truc</h1>
   <?php
   echo $form->create("Truc",array('action' => 'search'));
    echo $form->input("q", array('label' => 'Search for'));
    echo $form->end("Search"); 
    ?>
 
<div class="actions">
	<ul>
		<li><?php echo $html->link(sprintf(__('New %s', true), __('Truc', true)), array('action'=>'add')); ?></li>
	</ul>
</div>
<table>
 
	<tr>
<th>view</th>
		<th>id</th>
	<th>login</th>
	<th>User</th>
	<th>User2</th>
	<th>User3</th>
	<th>Db</th>
	<th>remarques</th>
	<th>Type</th>
	<th>version</th>
	<th>email</th>
	<th>homedir</th>
	<th>website</th>
	<th>date_modif</th>
                <th>Action</th>
	</tr>
 
<!-- Here's where we loop through our $results array, printing out post info -->
 
<?php foreach ($results as $post): ?>
	<tr>
	<td>
			<?php echo $html->link(__('View', true), array('action'=>'view', $post['truc']['id'])); ?>
	</td>
		<td>
			<?php echo $post['truc']['id']; ?>
		</td>
		<td>
			<?php echo $post['truc']['login']; ?>
		</td>
		<td>
			<?php echo $post['truc']['User']; ?>
		</td>
		<td>
			<?php echo $post['truc']['User2']; ?>
		</td>
		<td>
			<?php echo $post['truc']['User3']; ?>
		</td>
		<td>
			<?php echo $post['truc']['Db']; ?>
		</td>
		<td>
			<?php echo $post['truc']['remarques']; ?>
		</td>
		<td>
			<?php echo $post['truc']['Type']; ?>
		</td>
		<td>
			<?php echo $post['truc']['version']; ?>
		</td>
		<td>
			<?php echo $post['truc']['email']; ?>
		</td>
		<td>
			<?php echo $post['truc']['homedir']; ?>
		</td>
		<td>
			<?php echo $post['truc']['website']; ?>
		</td>
		<td>
			<?php echo $post['truc']['date_modif']; ?>
		</td>
 
 
 
                <td>
			<?php echo $html->link(
				'Delete', 
				"/trucs/delete/{$post['truc']['id']}", 
				null, 
				'Are you sure?'
			)?>
			<?php echo $html->link('Edit', '/trucs/edit/'.$post['truc']['id']);?>
		</td>
		<!-- <td><?php echo $post['truc']['created']; ?></td>-->
	</tr>
<?php endforeach; ?>
</table>

Dans le même répertoire, placer dans index.ctp au début:

   <?php
   echo $form->create("Truc",array('action' => 'search'));
    echo $form->input("q", array('label' => 'Search for'));
    echo $form->end("Search"); 
    ?>

todo: écrire un script pour faire ça, prévoir 2 types de recherche:

  • booléen (OR)
  • advanced (possibilité de remplir par champ le formulaire, AND)

select

http://book.cakephp.org/fr/view/728/select

select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)

Crée un menu de sélection, composé des éléments de $options, avec l'option spécifiée par $selected qui sera le champ sélectionné par défaut. Définissez $showEmpty à false si vous ne voulez pas afficher le champ vide.

<?php
 $options=array('M'=>'Homme','F'=>'Femme');
 echo $form->select('sexe',$options)
?>

affiche

<select name="data[User][sexe]" id="UserSexe">
<option value=""></option>
<option value="M">Homme</option>
<option value="F">Femme</option>
</select>

escape

pour échapper les caractères, p. ex. ' dans les requêtes sql mettre en haut du controller

<?php
	App::import('Sanitize');
class DonneesController extends AppController {
...

puis

$statut=Sanitize::escape($this->data['Donnee']['statut']);

sanitize

idem que pour escape, mais mettre

$statut=Sanitize::html($this->data['Donnee']['statut']);
  • info/faire_un_moteur_de_recherche_avec_cakephp.txt
  • Dernière modification : 2018/07/18 09:45
  • de radeff