public function processSearchToken($token, array $attributes, $tablePrefix = null)
    {
        /** @var \yii\db\Schema $schema */
        $schema = $this->getDb()->getSchema();
        $tablePrefix = $schema->quoteSimpleTableName($tablePrefix === null ? 't' : $tablePrefix);
        $conditions = ['or'];
        $formats = $this->attributeFormats();
        Yii::trace(
            'Processing token ' . $token . ', safe attributes: ' . print_r($attributes, true),
            'netis.crud.ActiveRecord'
        );
        // to support searching in enums token must be first translated to matching values
        $plainAttributes = [];
        $formatter = Yii::$app->formatter;
        /** @var EnumCollection $enums */
        $enums = $formatter instanceof Formatter ? $formatter->getEnums() : null;
        foreach ($attributes as $attribute) {
            if (!isset($formats[$attribute]) || $enums === null
                || is_array($formats[$attribute]) || !$enums->has($formats[$attribute])
            ) {
                $plainAttributes[$attribute] = $token;
                continue;
            }

            // do a linear search in map values and then use matching in query criteria
            $matching = [];
            foreach ($enums->get($formats[$attribute]) as $key => $label) {
                if (mb_stripos($label, $token, 0, 'UTF-8') !== false) {
                    $matching[] = $key;
                }
            }
            if (!empty($matching)) {
                // don't validate, because rules only allow one value anyway
                $conditions[] = ['in', $tablePrefix.'.'.$schema->quoteSimpleColumnName($attribute), $matching];
            }
        }
        $oldAttributes = $this->getAttributes($attributes);
        $this->setAttributes($plainAttributes);
        $this->validate($attributes);
        $validAttributes = array_diff($attributes, array_keys($this->getErrors()));
        Yii::trace(
            'Processing token ' . $token . ', validated in: ' . print_r($validAttributes, true),
            'netis.crud.ActiveRecord'
        );
        $attributeValues = $this->getAttributes($validAttributes);
        foreach ($validAttributes as $attribute) {
            $value = $attributeValues[$attribute];
            if (empty($value) || !isset($formats[$attribute])
                || ($enums !== null && !is_array($formats[$attribute]) && $enums->has($formats[$attribute]))
            ) {
                continue;
            }

            $conditions[] = $this->getAttributeCondition($attribute, $value, $formats, $tablePrefix, $this->getDb());
        }
        $this->setAttributes($oldAttributes);

        return $conditions !== ['or'] ? $conditions : null;
    }
public function processSearchToken($token, array $attributes, $tablePrefix = null)
    {
        /** @var \yii\db\Schema $schema */
        $schema = $this->getDb()->getSchema();
        $tablePrefix = $schema->quoteSimpleTableName($tablePrefix === null ? 't' : $tablePrefix);
        $conditions = ['or'];
        $formats = $this->attributeFormats();
        Yii::trace(
            'Processing token ' . $token . ', safe attributes: ' . print_r($attributes, true),
            'netis.crud.ActiveRecord'
        );
        // to support searching in enums token must be first translated to matching values
        $plainAttributes = [];
        $formatter = Yii::$app->formatter;
        /** @var EnumCollection $enums */
        $enums = $formatter instanceof Formatter ? $formatter->getEnums() : null;
        foreach ($attributes as $attribute) {
            if (!isset($formats[$attribute]) || $enums === null
                || is_array($formats[$attribute]) || !$enums->has($formats[$attribute])
            ) {
                $plainAttributes[$attribute] = $token;
                continue;
            }

            // do a linear search in map values and then use matching in query criteria
            $matching = [];
            foreach ($enums->get($formats[$attribute]) as $key => $label) {
                if (mb_stripos($label, $token, 0, 'UTF-8') !== false) {
                    $matching[] = $key;
                }
            }
            if (!empty($matching)) {
                // don't validate, because rules only allow one value anyway
                $conditions[] = ['in', $tablePrefix.'.'.$schema->quoteSimpleColumnName($attribute), $matching];
            }
        }
        $oldAttributes = $this->getAttributes($attributes);
        $this->setAttributes($plainAttributes);
        $this->validate($attributes);
        $validAttributes = array_diff($attributes, array_keys($this->getErrors()));
        Yii::trace(
            'Processing token ' . $token . ', validated in: ' . print_r($validAttributes, true),
            'netis.crud.ActiveRecord'
        );
        $attributeValues = $this->getAttributes($validAttributes);
        foreach ($validAttributes as $attribute) {
            $value = $attributeValues[$attribute];
            if (empty($value) || !isset($formats[$attribute])
                || ($enums !== null && !is_array($formats[$attribute]) && $enums->has($formats[$attribute]))
            ) {
                continue;
            }

            $conditions[] = $this->getAttributeCondition($attribute, $value, $formats, $tablePrefix, $this->getDb());
        }
        $this->setAttributes($oldAttributes);

        return $conditions !== ['or'] ? $conditions : null;
    }
public function processSearchToken ( $ token , array $ attributes , $ tablePrefix = null ) { /** @var \yii\db\Schema $schema */ $ schema = $ this -> getDb ( ) -> getSchema ( ) ; $ tablePrefix = $ schema -> quoteSimpleTableName ( $ tablePrefix === null ? 't' : $ tablePrefix ) ; $ conditions = [ 'or' ] ; $ formats = $ this -> attributeFormats ( ) ; Yii :: trace ( 'Processing token ' . $ token . ', safe attributes: ' . print_r ( $ attributes , true ) , 'netis.crud.ActiveRecord' ) ; // to support searching in enums token must be first translated to matching values $ plainAttributes = [ ] ; $ formatter = Yii :: $ app -> formatter ; /** @var EnumCollection $enums */ $ enums = $ formatter instanceof Formatter ? $ formatter -> getEnums ( ) : null ; foreach ( $ attributes as $ attribute ) { if ( ! isset ( $ formats [ $ attribute ] ) || $ enums === null || is_array ( $ formats [ $ attribute ] ) || ! $ enums -> has ( $ formats [ $ attribute ] ) ) { $ plainAttributes [ $ attribute ] = $ token ; continue ; } // do a linear search in map values and then use matching in query criteria $ matching = [ ] ; foreach ( $ enums -> get ( $ formats [ $ attribute ] ) as $ key => $ label ) { if ( mb_stripos ( $ label , $ token , 0 , 'UTF-8' ) !== false ) { $ matching [ ] = $ key ; } } if ( ! empty ( $ matching ) ) { // don't validate, because rules only allow one value anyway $ conditions [ ] = [ 'in' , $ tablePrefix . '.' . $ schema -> quoteSimpleColumnName ( $ attribute ) , $ matching ] ; } } $ oldAttributes = $ this -> getAttributes ( $ attributes ) ; $ this -> setAttributes ( $ plainAttributes ) ; $ this -> validate ( $ attributes ) ; $ validAttributes = array_diff ( $ attributes , array_keys ( $ this -> getErrors ( ) ) ) ; Yii :: trace ( 'Processing token ' . $ token . ', validated in: ' . print_r ( $ validAttributes , true ) , 'netis.crud.ActiveRecord' ) ; $ attributeValues = $ this -> getAttributes ( $ validAttributes ) ; foreach ( $ validAttributes as $ attribute ) { $ value = $ attributeValues [ $ attribute ] ; if ( empty ( $ value ) || ! isset ( $ formats [ $ attribute ] ) || ( $ enums !== null && ! is_array ( $ formats [ $ attribute ] ) && $ enums -> has ( $ formats [ $ attribute ] ) ) ) { continue ; } $ conditions [ ] = $ this -> getAttributeCondition ( $ attribute , $ value , $ formats , $ tablePrefix , $ this -> getDb ( ) ) ; } $ this -> setAttributes ( $ oldAttributes ) ; return $ conditions !== [ 'or' ] ? $ conditions : null ; }
Assigns specified token to specified attributes and validates
current model to filter the values. Then, creates search condition.
@param  string $token     one search token extracted from whole term
@param  array $attributes attributes safe to search
@param  string $tablePrefix
@return array all conditions joined with OR operator, should be merged with main query object
Assigns specified token to specified attributes and validates current model to filter the values . Then creates search condition .