phpmedoo

Best way to avoid specific values, when extracting from database?


The Problem

Ignoring specific values, when arrays are pulled, using medoo, and merged.

The Details

Say I have two arrays, created from two similar tables. They have a 'user'-row and an 'id'-row. Example below:

id        name   
-------   ---------
  1         John Doe     
  2         John Snow    
  3         Jane     
  4         Jane       

What is the best way to ignore all entries that have 'John ...' in the name row? My array_merge is currently like this:

  public function getUsersByName($name)
    {
        $res = array_merge(
            $this->_db->select('users1', ['id', 'name'],
            $this->_db->select('users2', ['id', 'name']
        );
        return $res;
    }

This returns a nice array, with all values in the tables. What is the best practice to ignore those values, and skip to the next legitimate value?


Solution

  • To run queries with WHERE .. NOT LIKE .. in medoo, you can pass a third parameter to select() that contains an array of the where clauses. The key is the column with the operator, and the value is - well, the value.

    For example, to select from users1 where no names begin with John, you'll need

    $this->_db->select('users1', ['id', 'name'], ['name[!~]' => 'John']);
    

    The [!~] signalizes NOT LIKE.