With in-query search like this I use named parameters - ie
/locations/find/term:leeds/field:city

This way the parameter order is not important, and it is easy to
substitute default values for anything not provided.  My specific
implementation is something like:


        function index()
        {
                $conditions = array();

                // get any conditions from the search / find url parameters
                $url_conditions = $this->_url_conditions('MyModel');
                if ( !empty($url_conditions) ){
                        $conditions[] = $url_conditions;
                }

                $data = $this->MyModel->findAll( $conditions );

                $view_data = array(
                        'data' => $data
                );
                $this->set( $view_data );
        }


        function _url_conditions( $model )
        {
                $conditions = array();

                // check if named parameters were provided
                $passed_search_term = val( @$this->passedArgs['search'], @$this-
>passedArgs['find'] );

                if ( !empty($passed_search_term) ){
                        $passed_search_term = urldecode($passed_search_term);

                        // we want to search for this value
                        loadModel( $model );
                        $this->{$model} = new $model();

                        $search_fields = array();
                        $search_conditions = array();

                        if ( !empty($this->passedArgs['field']) ){
                                // a specific field was given
                                $search_fields = array( 
$this->passedArgs['field'] );

                        } else {
                                // use all of the model's search fields
                                if ( !empty($this->{$model}->searchFields) and 
is_array($this-
>{$model}->searchFields) ){
                                        $search_fields = 
$this->{$model}->searchFields;
                                } else {
                                        $search_fields = array( 'title', 
'name', 'description' );
                                }
                        }

                        // now check that these fields exist
                        foreach ( $search_fields as $search_field ){
                                if ( $this->{$model}->hasField($search_field) ){
                                        $field_type = 
$this->{$model}->getColumnType($search_field);

                                        if ( $field_type == 'string' or 
$field_type == 'text' ){
                                                $search_conditions[ 
$model.'.'.$search_field ] = 'LIKE %'.
$passed_search_term.'%';
                                        } else {
                                                $search_conditions[ 
$model.'.'.$search_field ] =
$passed_search_term;
                                        }
                                }
                        }

                        if ( !empty($search_conditions) ){
                                if (count($search_conditions) > 1){
                                        $conditions['OR'] = $search_conditions;
                                } else {
                                        $conditions = $search_conditions;
                                }
                        }
                }

                return $conditions;
        }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to