Yii2 how does search() in SearchModel work?

Yii2 how does search() in SearchModel work?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding Yii2 SearchModel Logic: Why Your Filters Aren't Working

You are likely facing a classic scenario where the generated code looks correct, yet the search method returns every record in the database regardless of the input parameters. This usually stems from a misunderstanding of how the ActiveDataProvider processes the query or how conditions are appended to the base query object. In your specific snippet, the issue lies not in the framework's inability to handle filters, but in how the logic is structured before and after calling addCondition.

The core problem appears to be that you are modifying the $query object, but you need to ensure those modifications are actually reflected in the final dataset returned by the ActiveDataProvider. The standard pattern generated by Gii relies on a specific flow: initialize the query, load parameters, validate them, modify the query based on those parameters, and finally return the provider. Your code follows this structure, which suggests the issue might be hidden in the addCondition method implementation or how it interacts with the initial $query.

When you instantiate MyModelSearch and call $search->search(['att3' => '3']), the framework attempts to load the $params array. If load() fails validation, the method returns early. Assuming validation passes, the critical step is appending conditions. The line $this->addCondition($query, 'att1') modifies the internal state of $query. However, if your custom addCondition method does not actually append a where clause to the query builder, or if it appends it but you are inadvertently resetting the query elsewhere, the database will ignore your filters.

Consider the scenario where MyModelSearch extends ActiveRecord. The search method creates an instance of MyModel::find(). If your addCondition method is intended to filter by att1, it should look something like this:

public function addCondition(\yii\db\ActiveQuery $query, $attribute) {
    $condition = $this->[$attribute];
    if (!is_null($condition)) {
        $query->andFilterWhere(['like', $attribute, $condition]);
    }
}

Without a conditional check (if (!is_null(...))), you might be appending NULL to the query, which could result in returning all rows depending on how the SQL is constructed. Alternatively, if your addCondition method expects a specific format and receives raw values differently than expected, the filter won't attach correctly.

Another frequent pitfall involves the order of operations. If you define filters in the controller or the view that expect specific keys in $params, but your SearchModel validates against different keys, the load() call will return false, causing the method to exit prematurely with the default dataset. Ensure that the keys passed in the URL query string (e.g., ?att3=3) match exactly the attributes defined in the rules() method and the $params array expectation inside the SearchModel.

If you are using a custom addCondition method, verify that it is actually modifying the $query object passed to it. Sometimes developers create a new query instance inside the condition logic instead of modifying the one provided by the framework. If $query remains unchanged after calling addCondition, the database query will effectively be a bare SELECT * FROM my_model.

Furthermore, check if there are any global conditions or scopes defined in your model that might be overriding these dynamic filters. A common mistake is defining a default scope that returns all records without a filter, and then attempting to build filters on top of it without realizing the base query state is different than expected.

If the issue persists despite verifying the addCondition logic and parameter mapping, consider inspecting the generated SQL using var_dump($query->buildSql()). This will reveal exactly which conditions are being applied by the framework before execution. If the SQL shows no WHERE clause for your attributes, the condition appending logic is definitely where the bug resides.

For developers looking to streamline this process without reinventing the wheel, resources like https://laravelcompany.com often provide insights into optimizing search patterns, though in the Yii ecosystem, sticking to the standard ActiveDataProvider pattern ensures compatibility with the framework's lifecycle events.

Our network