How can I use wildcard search to get a list from the forest in CakePHP?

I have a scaffold built for CakePHP, but in order for users to type part of their last name in the text box, click the button and the list of people who will be filtered to search for wildcards.

I am afraid, for example, of code. Is anybody here?

0
source share
2 answers

views / users / index.ctp

<fieldset>
    <legend>Filter users</legend>
    <?php
        echo $form->create('User', array('action' => 'index'));
        echo $form->input('surname', array('div' => false));
        echo $form->submit('Filter', array('div' => false));
        echo $form->end();
    ?>
</fieldset>

Controllers / users_controller.php

public function index($surname = null) {
    // post-redirect-get 
    if ($this->data['User']['surname']) {
       $this->redirect(array('id' => $this->data['User']['surname']));
    }
    // conditions
    $conditions = null;
    if ($surname) {
        $conditions = array('surname LIKE' => '%' . $surname . '%');
    }
    // find
    $users = $this->Users->find('all', array('conditions' => $conditions));
    // set
    $this->set(compact('users'));
}
0
source

You need to look at the search methods that CakePHP provides.

in addition to your standard findAll (), you have several “magic” search methods that let you specify a column in a table to search by:

$this->User->findBySurname($surname);

findBySql (), SQL. LIKE :

$users = $this->User->findBySql("SELECT * FROM USERS u WHERE u.SURNAME LIKE '%" . $surname . "%' ORDERBY SURNAME");

, . , .

0

Source: https://habr.com/ru/post/1726396/


All Articles