Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Can't make the SearchForm work properly


Reply


2 Posts   1381 Views

Avatar
Mysth-R

Community Member, 3 Posts

29 July 2009 at 9:38pm

Edited: 29/07/2009 9:39pm

Hi,

I am trying to make a searchForm in my website, but on some keywords, it doesn't work properly.
Here is the structure :

Agence.php
AgenceParis.php
Page.php
Ville.php

The search form is displayed on the home page.

1 Agence can have many AgenceParis and many Ville.
1 AgenceParis can have one Agence and many Ville.
1 Ville can have one Agence and one AgenceParis.

_______________> Ville
Agence / /
\ _____> AgenceParis /

In the Agence.php I defined :

   static $has_many = array (
      'Villes'=>'Ville',
      'AgencePariss'=>'AgenceParis',
   );

   static $searchable_fields = array(
   'Ville.Nom',
//           'Ville.codepostal',
          'AgenceParis.InfoBulle'
);

In the AgenceParis.php :

static $has_many = array (
      'Villes'=>'Ville',
   );

   static $has_one = array (
         'MyAgence'=>'Agence'
   );
   static $searchable_fields = array(
   'Ville.Nom',
   'AgenceParis.InfoBulle'
//           'Ville.codepostal'
);

In the Ville.php :

   static $has_one = array (
         'MyAgencePage'=>'Agence',
         'MyAgenceParisPage'=>'AgenceParis'
   );

   static $searchable_fields = array(
   'Ville.Nom',
//           'Ville.codepostal'
);

And now this is how I define the search engine in Page.php :

function SearchForm() {
      $context = singleton('Ville')->getDefaultSearchContext();
      $fields = $context->getSearchFields();
      $actions = new FieldSet(new FormAction('doSearch', 'ok'));
      $form = new SearchForm($this, "SearchForm", $fields, $actions);
      return $form;
   }

   public function doSearch($data, $form) {
      $context = singleton('Ville')->getDefaultSearchContext();
      $results = $context->getResults($data);
      return $this->customise(array(
         'Results' => $results
      ))->renderWith( array ('Page_results', 'Page'));
   }

To resume.
I got many Agences which owns many Villes. (cities)
But for Paris, I got severals Agences. So for the Agences named Paris I got several AgenceParis which each owns Villes (cities).

When I do a search on a city of an agency the result is ok
(ex: Aubagne dépend de l'agence "Marseille") Aubagne is the city and the keyword and Marseille is the Agency)

When I do a search on a AgenceParis of an agency, the result is also ok
(ex : Versailles & alentours dépend de l'agence "Paris et Ile de France") Versaille & alentours is the AgenceParis, Versaille is the keyword and Paris et Ile de France is the Agency
But when I do a search on a city of an AgenceParis, this look like :
(ex : Elancourt dépend de l'agence "" and this link points to http:///) elancourt is a ville wich depend on a Agence Paris.

I don't really know what is wrong in my code.
Thank you for helping me.

Erwan.

Avatar
Mysth-R

Community Member, 3 Posts

31 July 2009 at 1:01am

Well, I solved my problem :)

The problem was in the template. Adding a condition to display AgenceParis when Agence = "" did it.
Thank you Digo who helped me on IRC.

Erwan.