findAll() in yii

findAll() in yii

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

Stefan Bogdanescu

Founder & Senior Architect

Why findAll() with Criteria Isn't Returning Your Data in Yii

You have a clear requirement: retrieve records from the EmailArchive table where the email_id matches a specific value, say 101. The schema is straightforward, and you've constructed a query using CDbCriteria. However, the code snippet provided yields no results. This isn't a mystery of complex database indexing or hidden column constraints; it is a fundamental mismatch between the logic in your SQL generation and the data you are asking for.

The issue lies in this line:
$criteria->addCondition("email_id < :email_id");

You are instructing Yii to generate an SQL WHERE clause that filters for records where email_id is strictly less than 101. When you pass $id = 101, the resulting query becomes SELECT * FROM email_archive WHERE email_id < 101. Looking at your sample data, there are no rows in the database with an email_id of 99 or lower. The only row matching your target is the one where email_id = 101, but your condition explicitly excludes equality and anything greater.

To fix this, you must change the operator from < to =. While it might seem trivial, debugging issues in Yii often comes down to verifying exactly how the Criteria object translates into SQL. If you are using Yii 2, the class name would be DbCriteria, but the logic remains identical. The corrected implementation looks like this:

$id = 101;
$criteria = new CDbCriteria(); // Or 'new DbCriteria()' in Yii 2
$criteria->addCondition("email_id = :email_id");
$comments = EmailArchive::model()->findAll($criteria, array(':email_id' => $id));

Why Not Use where() Directly?

While the Criteria approach works fine for complex queries involving joins or aggregations, using the fluent interface method often leads to cleaner, more readable code for simple lookups. Instead of building a criteria object just to pass one condition, you can chain the query directly:

$id = 101;
$comments = EmailArchive::model()
    ->findAll(array('condition' => "email_id = :id", 'params' => array(':id' => $id)));

Or, if you are comfortable with raw SQL conditions for this specific case:

$id = 101;
$comments = EmailArchive::model()->findAll("WHERE email_id = ?", array($id));

The trade-off here is flexibility. The Criteria class allows you to easily add sorting ($criteria->order), limiting results ($criteria->limit), and adding complex AND/OR logic without chaining multiple method calls. If your application requires frequent pagination or sorting on the email archive, sticking with CDbCriteria (or DbCriteria) is the better architectural choice. However, for a single-point lookup like this, the direct condition approach reduces boilerplate.

A Note on Parameter Binding

One of the most common pitfalls when using $criteria->addCondition with raw strings is forgetting to use bound parameters. In your original snippet, you used :email_id. This is good practice because it protects against SQL injection and prevents type coercion issues. If you were to write "email_id = $id" directly into the condition string, Yii might try to treat 101 as a string or concatenate it incorrectly depending on the version and configuration. Always ensure that any variables used in your addCondition string are passed via the second argument of findAll() using the correct placeholder syntax.

Moving to Active Record

Finally, consider whether you even need findAll(). If you know exactly one record exists for a given email_id, retrieving an array of records is unnecessary overhead. The findByPk or findOne methods are optimized for single-object retrieval and return the model instance directly if found, or null if not.

$emailArchive = EmailArchive::model()->findByCondition("email_id = :id", array(':id' => 101));

This pattern is particularly useful when you need to immediately access properties like $emailArchive->from or $emailArchive->to. If that single record does not exist, findAll() would return an empty array, whereas findOne returns null. Understanding the difference between these return types helps prevent potential "undefined property" errors later in your application logic.

For more advanced querying patterns and best practices in modern PHP frameworks, resources like https://laravelcompany.com often provide excellent comparisons on how similar concepts are handled in other ecosystems, which can inspire cleaner implementations even when sticking with Yii.

Our network