Search code examples
phpcakephpsearchcakedc

CakeDC search for cakephp orConditions - are there andConditions?


Ok, the case is the following:

There is a form that has a some drop downs and multiple selection fields.

All fine by now, except that I have to do the search by property type to be and/or and not only or as for the rest of the form (or at least I believe it is or for the rest).

i.e. property type 1 && property type 2 && (subtype 1 || sybtype 2 || sybtype 3)

Another thing is that when selecting all subtypes, the results shown should be the same as if none are selected.

I really have no idea where to read or look at for more detailed tutorial on the search functionality provided by this plugin in regards to "and" searches.

What I have in my model is the following:

    public $filterArgs = array(
        //array('name' => 'price', 'type' => 'query', 'method' => 'filterTitle'),
        array('name' => 'province_id', 'type' => 'value'),
        array('name' => 'city_id', 'type' => 'value'),
        array('name' => 'quarter_id', 'type' => 'value'),
        array('name' => 'refid', 'type' => 'value'),
        array('name' => 'to', 'type' => 'value'),
        array('name' => 'price1', 'name2' => 'price2', 'type' => 'expression', 'method' => 'priceRange', 'field' => 'Offer.price BETWEEN ? AND ?'),
        array('name' => 'area1', 'name2' => 'area2', 'type' => 'expression', 'method' => 'areaRange', 'field' => 'Offer.area BETWEEN ? AND ?'),
        array('name' => 'type_id', 'type' => 'query', 'method' => 'ManyOrConditions'),
        array('name' => 'subtype_id', 'type' => 'query', 'method' => 'ManyOrConditions'),
        array('name' => 'feature_id', 'type' => 'subquery', 'method' => 'findByTags1', 'field' => 'Offer.id'),
    );

   public function ManyOrConditions($data = array()) {
        //debug($data);
        $filter = $data['type_id'];
        //not sure if this works
        //$subtype = $data['subtype_id'];

        $cond = array(
            'OR' => array(
                $this->alias . '.type_id ' => $filter,
                //not sure if the below will work?
                //$this->alias . '.subtype_id ' => $subtype
        ));
        //debug($cond);
        return $cond;
    }

    public function orConditions($data = array()) {
        //debug($data);
        $filter = $data['type_id'];
        $cond = array(
            'OR' => array(
                $this->alias . '.type_id LIKE' => '%' . $filter . '%',
            //$this->alias . '.body LIKE' => '%' . $filter . '%',
        ));
        return $cond;
    }

Where it as I understand it is creating only "or" searches for the type_id ... sorry guys really am lost in this.

I am new to cakephp and so far have been able to read through thousands of lines of code and do a lot of changes to this system but here I just have no clue what to do due to lack of documentation on this plugin :(

I will appreciate any guidance to tutorials and/or proper documentation - not this one https://github.com/CakeDC/search.

If this is the right one though, please let me know what I am missing.

P.S. If you need any other code here, just let me know and I will provide it.


Solution

  • Thank you very much for your directions burzum. I finally understood it. Here is how I did it:

        public function andTypeConditions($data = array()){
    
            //The below two variables contain the data received by the submission of the form
            $typeID = $data['type_id'];
            $subTypeID = $data['subtype_id'];
            //Define a variable $conditions to return. The conditions you can build according CakePHP manual for complex find conditions        
            if (isset($data['subtype_id'])) {
                $conditions = array(
                    'Offer.type_id' => $typeID,
                    'Offer.subtype_id' => $subTypeID
                );            
            }  else {
                $conditions = array(
                    'Offer.type_id' => $typeID
                );
            }
    
            return $conditions;
        }
    

    For anyone that struggles with the same issue to clarify:

    In order to do an "AND" search in cakeDC search plugin you need to create a method that you will later on assign to both (in my case two but if you have more , to all) fields like so:

    public $filterArgs = array(
        array('name' => 'type_id', 'type' => 'query', 'method' => 'andTypeConditions'),
        array('name' => 'subtype_id', 'type' => 'query', 'method' => 'andTypeConditions'),
    );
    

    ... the method itself contains the comments you need to be able to adjust it for your needs.