How to override fields method in rest api


(Desaikalpesh34) #1

hello guys,

I want two fields method in model like


class User extends ActiveRecord

{

  public function fields() {

        return ['id' => 'id', 'title' => 'title', 'image' => 'fullpathImage'];

    }


   public function fields2() {

        return ['id' => 'id', 'title' => 'title'];

    }

}

how can i call this two fields method while rendering data in rest api controller like


return ['status' => true, 'message' => 'Category list', 'data' => Category::find()->orderBy('title')->all()];

its always return fields() method variable here i want override fields2()

How can i achive this any suggestion…


(Softark) #2

I don’t think there’s a convenient way to switch between “fields” and “fields2” in a model, because the name “fields” has a special meaning for a model.

I think you can consider using "extraFields".




class Category extends ActiveRecord

{

    public function fields() {

        return ['id' => 'id', 'title' => 'title'];

    }


    public function extraFields() {

        return ['image' => 'fullpathImage'];

    }

}



Or, if you want 2 kinds of "fields", you may extend the model itself.




class Category extends ActiveRecord

{

    public function fields() {

        return ['id' => 'id', 'title' => 'title', 'image' => 'fullpathImage'];

    }

}


...


class CategoryEx extends Category

{

   public function fields() {

        return ['id' => 'id', 'title' => 'title'];

   }

}



And




return ['status' => true, 'message' => 'Category list', 'data' => CategoryEx ::find()->orderBy('title')->all()];




(Desaikalpesh34) #3

Yes your suggestion is better but

SilverFire has suggest me something like

Check out Check Out

and i have tried


$this->serializer['fieldsParam'] ='field2';

but its not workng for me

am i missing something???


(Softark) #4

According to the Guide, you can do something like this:




use yii\rest\ActiveController;


class CategoryController extends ActiveController

{

    public $modelClass = 'app\models\Category';

    public $serializer = [

        'class' => 'yii\rest\Serializer',

        'fieldsParam' => 'fields2',

    ];

}



http://www.yiiframework.com/doc-2.0/guide-rest-response-formatting.html#data-serializing

I’m not sure if you can configure the serializer in the runtime as you have tried.


(Desaikalpesh34) #5

yes your point is perfect and right.

but i can’t figure out your last statement "I’m not sure if you can configure the serializer in the runtime as you have tried. "

when i print_r($this->serializer); its simply print yii\rest\Serializer string not object

am still missing something else??

Thanks for reply…


(Softark) #6

Try this:




$this->serializer = [

    'class' => 'yii\rest\Serializer',

    'fieldsParam' => 'fields2',

];

...

return [

    'status' => true,

    'message' => 'Category list',

    'data' => Category::find()->orderBy('title')->all()

];



According to the source code (https://github.com/yiisoft/yii2/blob/master/framework/rest/Controller.php), yii\rest\Controller uses the following method to serialize the data:

It tells us that an instance of the serializer will be created just before it serializes the data. So we can configure it on the fly.

And take a look at the API reference of Yii::createObject().

http://www.yiiframework.com/doc-2.0/yii-baseyii.html #createObject()-detail

The method can take a parameter of 1) a class name of the object, or, 2) a configuration array of the object.

The default value of yii\rest\Controller::serializer is a string "yii\rest\Serializer", but we can define it as a configuration array.

[P.S.]

This can be much simpler and cleaner than the first solution that I suggested.

I learned a lot through the conversation with you. :)


(Desaikalpesh34) #7

Thanks for your support its amazing and working!!!!!

Thank you once again :rolleyes:


(Femi) #9

@desalkalpesh34 Please, can you share your complete working solution?