Returns a list of active record models that match the specified primary key value(s) or a set of column values.
The method accepts:
- a scalar value (integer or string): query by a single primary key value and return an array containing the
corresponding record (or an empty array if not found).
- a non-associative array: query by a list of primary key values and return the
corresponding records (or an empty array if none was found).
Note that an empty condition will result in an empty result as it will be interpreted as a search for
primary keys and not an empty
- an associative array of name-value pairs: query by a set of attribute values and return an array of records
matching all of them (or an empty array if none was found). Note that
['id' => 1, 2] is treated as
a non-associative array.
Column names are limited to current records table columns for SQL DBMS, or filtered otherwise to be limted to simple filter conditions.
- a yii\db\Expression: The expression will be used directly. (@since 2.0.37)
This method will automatically call the
all() method and return an array of ActiveRecord
Note: As this is a short-hand method only, using more complex conditions, like ['!=', 'id', 1] will not work.
If you need to specify more complex conditions, use find() in combination with where() instead.
See the following code for usage examples:
$customers = Customer::findAll(10);
$customers = Customer::find()->where(['id' => 10])->all();
$customers = Customer::findAll([10, 11, 12]);
$customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
$customers = Customer::findAll(['age' => 30, 'status' => 1]);
$customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();
If you need to pass user input to this method, make sure the input value is scalar or in case of
array condition, make sure the array structure can not be changed from the outside:
public function actionView($id)
$model = Post::findOne($id);
$model = Post::findOne(['id' => Yii::$app->request->get('id')]);
$model = Post::findOne(Yii::$app->request->get('id'));
Signup or Login in order to comment.