FindAllByPk analog in Yii2

How can I write a query for ActiveRecord that will be analogous to findAllByPk function in yii?

I tried this:

$records = TableName::find($ids)->all();

But it does not work, returns all records.

+4
source share
2 answers

If $ ids is an array, if primary keys are like [1, 3, 5, 23]. You can use this

$entries = TableName::findAll($ids);

This is a short section for this syntax.

$entries = TableName::find()
                   ->where(['id'=>$ids])
                   ->all();
+3
source

Assuming what $idsis an array,

$entries = TableName::find()
                ->where(['id'=>$ids])
                ->all();

More examples in white papers .

+3
source

Source: https://habr.com/ru/post/1544633/


All Articles