Domanda

I was wondering how I would do a search on my home page for something like property.

I am trying to understand the concept and have viewed multiple website but I still can't understand how this would be done.

All I need is the push in the right direction. I need all cases where in relation to for example x model and a search on it. While mine would be a search performed from home for property.

Any help will be greatly appreciated.

The code which I have placed in the controller I am not sure is correct. Home Controller

public function index() {
        //Populate localities dropdownlist  
        App::import('model','locality');
        $locality = new Locality('locality');
        $this->set('localities',$locality->find('list', array('fields' => array('id', 'fullname'))));

        App::import('model','condition');
        $condition = new Condition('condition');
        $this->set('conditions',$condition->find('list', array('fields' => array('id', 'condition'))));

        App::import('model','category');
        $category = new Category('category');
        $this->set('categories',$category->find('list', array('fields' => array('id', 'category'))));

        App::import('model','serviceType');
        $servicetype = new ServiceType('serviceType');
        $this->set('serviceTypes',$servicetype->find('list', array('fields' => array('id', 'service_type'))));
    }

The view

<?php 
    echo $this->Form->create('Property',array('contoller' =>'home', 'action'=> 'search'));
    echo $this->Form->input('priceFrom',array('style'=> 'width:100%;'));
    echo $this->Form->input('priceTo',array('style'=> 'width:100%;'));
    echo $this->Form->input('locality_id',array('style'=> 'width:100%;'));
    echo $this->Form->input('condition_id',array('style'=> 'width:100%;'));
    echo $this->Form->input('category_id',array('style'=> 'width:100%;'));
    echo $this->Form->input('service_type_id',array('style'=> 'width:100%;'));
    echo $this->Form->end('Search');
    ?>
È stato utile?

Soluzione

You can create an element that handles the search functionality, targeting the form to a function inside a controller.

Say, if you want to search properties, you can do inside PropertyController:

public function search()
{
    //your search functionality, like $this->Property->find('all');, or adding conditions
}

Then you can add an element that has the search form, naming, say, search.ctp (inside View/Elements folder):

$this->Form->create('Property', array('type' => 'get'));
$this->Form->input('search');
$this->Form->end('Search');

And in your home page, you can add the element with:

echo $this->element('search');

The good thing about elements is that you can reuse it in other views, or in your layout

Altri suggerimenti

Something like this sounds like a good place to start (where the trick you're probably missing is using the ClassRegistry to init a different model, so you can perform queries on it)

<?php
class HomeController extends AppController {
    public function search() {
        if (!empty($this->request->query['s'])) {
            $property = ClassRegistry::init('Property');
            $searchstring = h($this->request->query['s']);
            $searchResults = $property->find('all',array(
                'limit' => 15,
                'order' => array(
                    'Property.title' => 'asc'
                ),
                'conditions' => array(
                    'or' => array(
                        "Property.title LIKE" => "%$searchstring%",
                        "Property.desc LIKE" => "%$searchstring%" //or whatevs
                    )
                )
            ));
            $this->set(compact('searchResults'));
        }
    }
}

will give search results on urls like: http://example.com/home/search?s=palacial

If you're worried about SQL injection, I've heard that:

"CakePHP handles SQL escaping on all parameters to Model::find() and Model::save()." http://book.cakephp.org/2.0/en/core-utility-libraries/sanitize.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top