Get data on basis of fields in Yii2

I am trying to get data on basis of fields in query param ie

users/1?fields=id,name

its give id and name using findOne

 User::findOne(1);

Result:

{
    "id": 12,
    "name": 'Jhon'
}

When

 users?field=id,name

Its give all fields of user Model using findAll()

 User::findAll([$ids])

Result:

[
 {
  'id': 1
   'name': abc
   'age':30
   'dob':1970
   'email':abc@test.com
 },
 {
  'id': 2
   'name': abc
   'age':30
   'dob':1970
   'email':abc1@test.com
 },

Why findAll() not work like findOne() result

Hi @Faizan_Khattak,

findAll() returns an array of ActiveRecord objects (or an empty array), while findOne() returns a single object (or a null).

@softark you didn’t understand my question?

Ah, so you want only id and name when you use findAll?

As far as I know, findOne should also return age, dob and email. So the difference should lie in your code, not in the difference between findOne and findAll.

Could you share your code?

@softark findAll should return only id and name

Please explain why you think it should.