How to apply two filters in a search field?

Asked

Viewed 316 times

1

I have a field nome_email, where the user enters any string and I need to be filtered everything that is placed in this field and display something corresponding to the NOME or EMAIL of the registered customer.

Any idea how to do this?

In accordance with 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')
                )
            )
        )
    );

"Let’s change our example to concatenate the filters by the OR rule, and if filter1 is informed we want only active users. From this we get the condition:"

WHERE ((User.name LIKE '%Pedro%') 
  AND (User.active = 1)) 
   OR (User.username LIKE '%elsner%')

So what I need is a Filtro whose condition returned would be something like:

WHERE ((User.name ILIKE '%Pedro%') OR (User.username ILIKE '%Pedro%'))
  • Wouldn’t it be better if you left 2 separate fields?

  • Maybe, but I need to do it like this.

1 answer

1


If you are using version 3.x of Cakephp you can do so:

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

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

I believe it also works in version 2.x.

Edited:

Adding content to the answer:

Watching your response, I tried to apply to the filter, doing so:

<?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%'
    )
)

So I go through these conditions and I check to see if $chave == 'OR' which 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 entered 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 the Paginator i apply the variable $conditions thus:

$this->Paginator->settings = array(
    'fields' => array( XXXXXX ),
    'joins' => array( XXXXXXX ),
    'conditions' => array($conditions)
);

  • Edited to apply what I really needed, because your answer was correct, but it wasn’t exactly what I needed. But thank you Jeferson Assis

Browser other questions tagged

You are not signed in. Login or sign up in order to post.