Alex
Alex

Reputation: 8072

findAllByPk analog in Yii2

How can i write query for ActiveRecord that will be an analog of findAllByPk function in yii?

I tried this:

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

But it doesn't work, returns all records.

Upvotes: 4

Views: 374

Answers (2)

Felipe
Felipe

Reputation: 11887

Assuming $ids is an array,

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

Lots more examples in the official docs.

Upvotes: 3

ahb360
ahb360

Reputation: 373

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

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

It is a short cut for this syntax

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

Upvotes: 3

Related Questions