MetaModels\Attribute\BaseSimple::searchFor PHP Метод

searchFor() публичный Метод

Base implementation, perform string matching search. The standard wildcards * (many characters) and ? (a single character) are supported.
public searchFor ( string $strPattern ) : int[]
$strPattern string The text to search for. This may contain wildcards.
Результат int[] the ids of matching items.
    public function searchFor($strPattern)
    {
        // Base implementation, do a simple search on given column.
        $objQuery = $this->getMetaModel()->getServiceContainer()->getDatabase()->prepare(sprintf('SELECT id FROM %s WHERE %s LIKE ?', $this->getMetaModel()->getTableName(), $this->getColName()))->execute(str_replace(array('*', '?'), array('%', '_'), $strPattern));
        $arrIds = $objQuery->fetchEach('id');
        return $arrIds;
    }

Usage Example

Пример #1
0
 /**
  * Search all items that match the given expression.
  *
  * Base implementation, perform string matching search.
  * The standard wildcards * (many characters) and ? (a single character) are supported.
  *
  * @param string $strPattern The text to search for. This may contain wildcards.
  *
  * @return int[] the ids of matching items.
  */
 public function searchFor($strPattern)
 {
     // If search with wildcard => parent implementation with "LIKE" search.
     if (false !== strpos($strPattern, '*') || false !== strpos($strPattern, '?')) {
         return parent::searchFor($strPattern);
     }
     // Not with wildcard but also not numeric, impossible to get decimal results.
     if (!is_numeric($strPattern)) {
         return array();
     }
     // Do a simple search on given column.
     $query = $this->getMetaModel()->getServiceContainer()->getDatabase()->prepare(sprintf('SELECT id FROM %s WHERE %s=?', $this->getMetaModel()->getTableName(), $this->getColName()))->execute($strPattern);
     return $query->fetchEach('id');
 }