How to apply two filters in a search field?

1

I have a nome_email field, where the user types a string and I need to filter everything that is placed in that field and display something corresponding to the NOME or EMAIL of the registered user.

Any ideas how to do this?

According to Pedro Elsner :

<?php
    $this->FilterResults->addFilters(
        array(
            'OR' => array(
                'filter1' => array(
                    'User.name' => array('operator' => 'LIKE'),
                    'User.active' => array('value' => '1')
                )
                'filter2' => array(
                'User.username' => array('operator' => 'LIKE')
                )
            )
        )
    );

asked by anonymous 28.12.2016 / 16:10

1 answer

1

If you are using version 3.x of CakePHP it can be done this way:

$user_email = $this->request->data('user_email');

$this->User->find('all', [
    'conditions' => [
        'OR' => [
            'User.name LIKE' => '%'.$user_email.'%',
            'User.email LIKE' => '%'.$user_email.'%',
            ]
        ]
    ]);

I think it works also in version 2.x.

Edited:

Adding content to the response:

Looking at your answer, I tried to apply it to the filter, doing this:

<?php
    $this->FilterResults->addFilters(
        array(
            'filter1' => array(
                'OR' => array(
                    'User.name' => array(
                        'operator' => 'ILIKE',
                        'value' => array(
                            'before' => '%',
                            'after' => '%'
                        )
                    ),
                    'User.username' => array(
                        'operator' => 'ILIKE',
                        'value' => array(
                            'before' => '%',
                            'after' => '%'
                        )
                    )
                )
            )
        )
    );

With this filter if I debug the same, I will have the following output:

debug($this->Filter->getConditions());
die();


saída:
------
array(
    'OR' => array(
        'User.nome ILIKE'  => '%valordigitado%',
        'User.username ILIKE' => '%valordigitado%'
    )
)

Then I go through these conditions and check that $chave == 'OR' is the result of the applied filter, so I assign its value to the filter variable as below:

$conditions = array();
$filter1 = '';

foreach ($this->Filter->getConditions() as $chave => $valor) {
    if ($chave == 'OR') {
        $filter1 = $valor;
    }
}

After assigning a variable the value that was typed in the field:

$nome_username = $this->request->data['filter']['filter1'];

Then I check if the variable $filter1 is empty and I apply the condition to put in Paginator

if (!empty($filter1)) {
    $conditions[] = "(User.nome ILIKE '%$nome_username%' 
                  OR User.username ILIKE '%$nome_username%')";
}

And in Paginator I apply the variable $conditions like this:

$this->Paginator->settings = array(
    'fields' => array( XXXXXX ),
    'joins' => array( XXXXXXX ),
    'conditions' => array($conditions)
);
    
06.01.2017 / 13:26