简体   繁体   中英

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).

ie 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.

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

AND conditions or any other customized query/condition would work exactly the same as orConditions. It is just a callback method in which you can do anything you want to build any kind of query you need.

See how "complex" conditions work: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

array(
    'OR' => array(
        array('Company.name' => 'Future Holdings'),
        array('Company.city' => 'CA')
    ),
    'AND' => array(
        array(
            'OR' => array(
                array('Company.status' => 'active'),
                'NOT' => array(
                    array('Company.status' => array('inactive', 'suspended'))
                )
            )
        )
    )
)

Just create a custom method "myCustomAnd" and put whatever query you need in there using the $data that is passed to the method.

The plugin comes along with a complete example I don't think it is bad documented. It is pretty obvious what you need to do IMHO.

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.

Here's what I ended up doing for a more complex search. Using the CakeDC Search plugin...this uses 'LIKE OR' instead of 'LIKE AND', but hopefully it sheds some light on the how to do more complex searches using the Search plugin.

User.search is the input element name in my submission form.

class User extends AppModel {
    public $filterArgs = array(
        'search' => array('type' => 'like', 'field' => array('User.name', 'User.email'))
    }
}

class UsersController extends AppController {
    public $components = array('Search.Prg');

    public function index() {
        $this->Prg->commonProcess();

        $this->User->data['User'] = $this->passedArgs;

        if ($this->User->Behaviors->loaded('Searchable')) {
           $query = $this->passedArgs;
           $parsedConditions = $this->User->parseCriteria($query);
       } else {
           $parsedConditions = array();
       }

       $this->Paginator->settings['User']['conditions'] = $parsedConditions;

       $this->set('users', $this->Paginator->paginate());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM