Class yii\data\ActiveDataFilter
ActiveDataFilter 允许以适合 \yii\data\yiidbQueryInterface::where() 的格式组合过滤条件。
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $activeValidators | yii\validators\Validator[] | The validators applicable to the current $scenario. | yii\base\Model |
| $attributeMap | array | 在搜索条件中实际使用的属性名称,格式如:[filterAttribute => actualAttribute]。
例如,在使用表连接搜索查询的案例中,属性映射可以像下面这样:
`php
[
'authorName' => '{{author}}. |
yii\data\DataFilter |
| $attributes | array | Attribute values (name => value). | yii\base\Model |
| $behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
| $conditionBuilders | array | 过滤条件关键字与构建方法之间的映射关系。
这些方法被 buildCondition() 方法用于构建实际的过滤条件。
可以使用 PHP callback 指定特定的条件生成器。 例如:
`php
[
'XOR' => function (string $operator, mixed $condition) {
//return array;
},
'LIKE' => function (string $operator, mixed $condition, string $attribute) {
//return array;
},
]
` |
yii\data\ActiveDataFilter |
| $conditionValidators | array | 过滤器条件关键字和校验方法的映射。 这些方法被 validateCondition() 方法使用以校验原始过滤条件。 | yii\data\DataFilter |
| $errorMessages | array | 错误信息,格式如:[errorKey => message]。 |
yii\data\DataFilter |
| $errors | array | Errors for all attributes or the specified attribute. | yii\base\Model |
| $filter | mixed | 原始过滤器值 | yii\data\DataFilter |
| $filterAttributeLabel | string | 通过 $filterAttributeName 指定的过滤器属性标签。 将在错误消息合成中使用。 | yii\data\DataFilter |
| $filterAttributeName | string | 处理过滤器值的属性名称。 这个名字用于通过 load() 方法加载数据。 | yii\data\DataFilter |
| $filterControls | array | 在过滤器中可能用到的关键字和表达式。
数组键是从用户请求中获取的在原始过滤器值中使用的表达式。
数组值是在该类方法中使用的内部构建的关键字。
任何未指定的关键字将不被识别为过滤器控件,同时都将被视为
属性名。因此我们应该避免过滤器控件关键字和属性名称之间的冲突。
如:我们指定了过滤器控件关键字 'like',同时也有一个属性叫做 'like',类似于这种属性指定条件是
不会生效的。
我们可以为同一个过滤器构建关键字指定一些关键字,创建多个别名。例如:
`php
[
'eq' => '=',
'=' => '=',
'==' => '=',
'===' => '=',
// . |
yii\data\DataFilter |
| $firstErrors | array | The first errors. | yii\base\Model |
| $iterator | ArrayIterator | An iterator for traversing the items in the list. | yii\base\Model |
| $multiValueOperators | array | 可接受多个值的操作符关键字列表。 | yii\data\DataFilter |
| $operatorTypes | array | 指定每个操作符支持的搜索属性类型的列表。 这个字段应该使用这种格式:'operatorKeyword' => ['type1', 'type2' . | yii\data\DataFilter |
| $queryOperatorMap | array | 将过滤操作符映射到 yii\db\QueryInterface::where() 中使用的操作符。
格式:[filterOperator => queryOperator]。
如果映射中没有出现特定的操作符关键字,则按原样使用它。
通常,由于过滤器操作符名称与 \yii\data\yiidbQueryInterface::where() 中使用的名称一致,所以
映射可以留空。然而,在某些特殊情况下,您可能需要对其进行调整。
例如,在使用 PosgreSQL 时,您可能希望设置以下映射:
`php
[
'LIKE' => 'ILIKE'
]
` |
yii\data\ActiveDataFilter |
| $scenario | string | The scenario that this model is in. | yii\base\Model |
| $searchAttributeTypes | array | 搜索属性类型映射。 | yii\data\DataFilter |
| $searchModel | yii\base\Model | 模型实例。 | yii\data\DataFilter |
| $validators | ArrayObject|yii\validators\Validator[] | All the validators declared in the model. | yii\base\Model |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __call() | Calls the named method which is not a class method. | yii\base\Component |
| __clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
| __construct() | Constructor. | yii\base\BaseObject |
| __get() | Returns the value of a component property. | yii\base\Component |
| __isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
| __set() | Sets the value of a component property. | yii\base\Component |
| __unset() | Sets a component property to be null. | yii\base\Component |
| activeAttributes() | Returns the attribute names that are subject to validation in the current scenario. | yii\base\Model |
| addError() | Adds a new error to the specified attribute. | yii\base\Model |
| addErrors() | Adds a list of errors. | yii\base\Model |
| afterValidate() | This method is invoked after validation ends. | yii\base\Model |
| attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
| attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
| attributeHints() | Returns the attribute hints. | yii\base\Model |
| attributeLabels() | Returns the attribute labels. | yii\base\Model |
| attributes() | Returns the list of attribute names. | yii\base\Model |
| beforeValidate() | This method is invoked before validation starts. | yii\base\Model |
| behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
| build() | 从 $filter 值构建实际的过滤器规范。 | yii\data\DataFilter |
| canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
| canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
| className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
| clearErrors() | Removes errors for all attributes or a single attribute. | yii\base\Model |
| createValidators() | Creates validator objects based on the validation rules specified in rules(). | yii\base\Model |
| detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
| detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
| ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
| extraFields() | Returns the list of fields that can be expanded further and returned by toArray(). | yii\base\ArrayableTrait |
| fields() | Returns the list of fields that should be returned by default by toArray() when no specific fields are specified. | yii\base\ArrayableTrait |
| formName() | Returns the form name that this model class should use. | yii\base\Model |
| generateAttributeLabel() | Generates a user friendly attribute label based on the give attribute name. | yii\base\Model |
| getActiveValidators() | Returns the validators applicable to the current $scenario. | yii\base\Model |
| getAttributeHint() | Returns the text hint for the specified attribute. | yii\base\Model |
| getAttributeLabel() | Returns the text label for the specified attribute. | yii\base\Model |
| getAttributes() | Returns attribute values. | yii\base\Model |
| getBehavior() | Returns the named behavior object. | yii\base\Component |
| getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
| getErrorMessages() | yii\data\DataFilter | |
| getErrorSummary() | Returns the errors for all attributes as a one-dimensional array. | yii\base\Model |
| getErrors() | Returns the errors for all attributes or a single attribute. | yii\base\Model |
| getFilter() | yii\data\DataFilter | |
| getFirstError() | Returns the first error of the specified attribute. | yii\base\Model |
| getFirstErrors() | Returns the first error of every attribute in the model. | yii\base\Model |
| getIterator() | Returns an iterator for traversing the attributes in the model. | yii\base\Model |
| getScenario() | Returns the scenario that this model is used in. | yii\base\Model |
| getSearchAttributeTypes() | yii\data\DataFilter | |
| getSearchModel() | yii\data\DataFilter | |
| getValidators() | Returns all the validators declared in rules(). | yii\base\Model |
| hasErrors() | Returns a value indicating whether there is any validation error. | yii\base\Model |
| hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
| hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
| hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
| init() | Initializes the object. | yii\base\BaseObject |
| instance() | 返回静态类实例,该实例可用于获取 meta 信息 | yii\base\StaticInstanceTrait |
| isAttributeActive() | Returns a value indicating whether the attribute is active in the current scenario. | yii\base\Model |
| isAttributeRequired() | Returns a value indicating whether the attribute is required. | yii\base\Model |
| isAttributeSafe() | Returns a value indicating whether the attribute is safe for massive assignments. | yii\base\Model |
| load() | Populates the model with input data. | yii\base\Model |
| loadMultiple() | Populates a set of models with the data from end user. | yii\base\Model |
| normalize() | 格式化过滤器质,根据 $filterControls 和 $attributeMap 方法替换原始关键字。 | yii\data\DataFilter |
| off() | Detaches an existing event handler from this component. | yii\base\Component |
| offsetExists() | Returns whether there is an element at the specified offset. | yii\base\Model |
| offsetGet() | Returns the element at the specified offset. | yii\base\Model |
| offsetSet() | Sets the element at the specified offset. | yii\base\Model |
| offsetUnset() | Sets the element value at the specified offset to null. | yii\base\Model |
| on() | Attaches an event handler to an event. | yii\base\Component |
| onUnsafeAttribute() | This method is invoked when an unsafe attribute is being massively assigned. | yii\base\Model |
| rules() | Returns the validation rules for attributes. | yii\base\Model |
| safeAttributes() | Returns the attribute names that are safe to be massively assigned in the current scenario. | yii\base\Model |
| scenarios() | Returns a list of scenarios and the corresponding active attributes. | yii\base\Model |
| setAttributes() | Sets the attribute values in a massive way. | yii\base\Model |
| setErrorMessages() | 设置验证结构过滤的错误信息响应列表,格式如:[errorKey => message]。
消息包含根据消息上下文填充的占位符。
对于每条消息,{filter} 占位符是可用的,参见 $filterAttributeName 属性标签 |
yii\data\DataFilter |
| setFilter() | yii\data\DataFilter | |
| setScenario() | Sets the scenario for the model. | yii\base\Model |
| setSearchAttributeTypes() | yii\data\DataFilter | |
| setSearchModel() | yii\data\DataFilter | |
| toArray() | Converts the model into an array. | yii\base\ArrayableTrait |
| trigger() | Triggers an event. | yii\base\Component |
| validate() | Performs the data validation. | yii\base\Model |
| validateFilter() | 验证过滤器属性值以匹配过滤器条件规范。 | yii\data\DataFilter |
| validateMultiple() | Validates multiple models. | yii\base\Model |
受保护的方法
Events
| 事件 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| EVENT_AFTER_VALIDATE | yii\base\Event | An event raised at the end of validate() | yii\base\Model |
| EVENT_BEFORE_VALIDATE | yii\base\ModelEvent | An event raised at the beginning of validate(). | yii\base\Model |
常量
| 常量 | 值 | 描述 | 被定义在 |
|---|---|---|---|
| SCENARIO_DEFAULT | 'default' | The name of the default scenario. | yii\base\Model |
| TYPE_ARRAY | 'array' | yii\data\DataFilter | |
| TYPE_BOOLEAN | 'boolean' | yii\data\DataFilter | |
| TYPE_DATE | 'date' | yii\data\DataFilter | |
| TYPE_DATETIME | 'datetime' | yii\data\DataFilter | |
| TYPE_FLOAT | 'float' | yii\data\DataFilter | |
| TYPE_INTEGER | 'integer' | yii\data\DataFilter | |
| TYPE_STRING | 'string' | yii\data\DataFilter | |
| TYPE_TIME | 'time' | yii\data\DataFilter |
属性详情
过滤条件关键字与构建方法之间的映射关系。 这些方法被 buildCondition() 方法用于构建实际的过滤条件。 可以使用 PHP callback 指定特定的条件生成器。 例如:
[
'XOR' => function (string $operator, mixed $condition) {
//return array;
},
'LIKE' => function (string $operator, mixed $condition, string $attribute) {
//return array;
},
]
public array $conditionBuilders = ['AND' => 'buildConjunctionCondition', 'OR' => 'buildConjunctionCondition', 'NOT' => 'buildBlockCondition', '<' => 'buildOperatorCondition', '>' => 'buildOperatorCondition', '<=' => 'buildOperatorCondition', '>=' => 'buildOperatorCondition', '=' => 'buildOperatorCondition', '!=' => 'buildOperatorCondition', 'IN' => 'buildOperatorCondition', 'NOT IN' => 'buildOperatorCondition', 'LIKE' => 'buildOperatorCondition']
将过滤操作符映射到 yii\db\QueryInterface::where() 中使用的操作符。
格式:[filterOperator => queryOperator]。
如果映射中没有出现特定的操作符关键字,则按原样使用它。
通常,由于过滤器操作符名称与 \yii\data\yiidbQueryInterface::where() 中使用的名称一致,所以 映射可以留空。然而,在某些特殊情况下,您可能需要对其进行调整。 例如,在使用 PosgreSQL 时,您可能希望设置以下映射:
[
'LIKE' => 'ILIKE'
]
方法详情
建立一个搜索条件的特殊属性。
| protected array buildAttributeCondition($attribute, $condition) | ||
| $attribute | string | 搜索属性名称。 |
| $condition | mixed | 搜索条件。 |
| return | array | 实际条件。 |
|---|---|---|
构建由单个条件组成的块条件。
它包括 not 操作符。
| protected array buildBlockCondition($operator, $condition) | ||
| $operator | string | 操作符关键字。 |
| $condition | mixed | 原始条件。 |
| return | array | 实际条件。 |
|---|---|---|
| protected array buildCondition($condition) | ||
| $condition | array | |
| return | array | 构建的条件 |
|---|---|---|
构建由多个独立条件组成的连接条件。
它包括 and 和 or 等操作符。
| protected array buildConjunctionCondition($operator, $condition) | ||
| $operator | string | 操作符关键字。 |
| $condition | mixed | 原始条件。 |
| return | array | 实际条件。 |
|---|---|---|
执行实际的过滤器构建。 默认情况下,此方法返回 normalize() 方法的返回值。 子类可以重写此方法,提供更具体的实现。
| protected mixed buildInternal() | ||
| return | mixed | 被构建的实际的过滤器值。 |
|---|---|---|
构建一个操作符条件。
| protected array buildOperatorCondition($operator, $condition, $attribute) | ||
| $operator | string | 操作符关键字。 |
| $condition | mixed | 属性条件。 |
| $attribute | string | 属性名字。 |
| return | array | 实际条件。 |
|---|---|---|