CI_DB_active_record::_like PHP Méthode

_like() public méthode

Called by like() or orlike()
public _like ( $field, $match = '', $type = 'AND ', $side = 'both', $not = '' ) : object
Résultat object
    function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
    {
        if (!is_array($field)) {
            $field = array($field => $match);
        }
        foreach ($field as $k => $v) {
            $k = $this->_protect_identifiers($k);
            $prefix = count($this->ar_like) == 0 ? '' : $type;
            $v = $this->escape_like_str($v);
            if ($side == 'before') {
                $like_statement = $prefix . " {$k} {$not} LIKE '%{$v}'";
            } elseif ($side == 'after') {
                $like_statement = $prefix . " {$k} {$not} LIKE '{$v}%'";
            } else {
                $like_statement = $prefix . " {$k} {$not} LIKE '%{$v}%'";
            }
            // some platforms require an escape sequence definition for LIKE wildcards
            if ($this->_like_escape_str != '') {
                $like_statement = $like_statement . sprintf($this->_like_escape_str, $this->_like_escape_chr);
            }
            $this->ar_like[] = $like_statement;
            if ($this->ar_caching === TRUE) {
                $this->ar_cache_like[] = $like_statement;
                $this->ar_cache_exists[] = 'like';
            }
        }
        return $this;
    }

Usage Example

 /**
  * Like
  *
  * Called by like() or orlike()
  *
  * @access    private
  * @param    mixed
  * @param    mixed
  * @param    string
  * @return    object
  */
 function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
 {
     // store this as the last_bracket_type
     $this->last_bracket_type = 'like';
     // do we already have entries for the where clause?
     if ($where_count = count($this->ar_where) > 0) {
         // yes, add a dummy entry to force the $type being added
         $this->ar_like[] = '';
     }
     // call the original method
     $result = parent::_like($field, $match, $type, $side, $not);
     // fetch the key of the last entry added
     end($this->ar_like);
     $key = key($this->ar_like);
     // do we need to add an open bracket
     if ($this->ar_bracket_open) {
         // was this the first entry?
         if ($where_count == 0) {
             // first where clause, simply prefix it with a bracket open
             $this->ar_like[$key] = '(' . $this->ar_like[$key];
         } else {
             // subsequent where clause, strip the type before adding the bracket open
             $this->ar_like[$key] = $type . ' (' . substr($this->ar_like[$key], strlen($type));
         }
         // reset the bracket state
         $this->ar_bracket_open = FALSE;
         if ($this->ar_caching === TRUE) {
             $this->ar_cache_like[$key] = $this->ar_like[$key];
         }
     }
     // add the like to the ar_where array to maintain where clause sequence
     $this->ar_where[] = $this->ar_like[$key];
     $this->ar_like = array();
     // return the result
     return $result;
 }