Reputation: 83
I'm using common Yii2 ActiveController
REST implementaion:
class ResultController extends ActiveController
{
public $modelClass = 'app\models\Result';
public function actionResultList($id)
{
/* @var $modelClass \yii\db\BaseActiveRecord */
$modelClass = $this->modelClass;
return new ActiveDataProvider...
It works all good for me, except one feature - I've got some JSON stored in my Result->rawJson
model as string. When I'm outputting it through this controller it gets escaped with slashes, and angular on frontend treats it as a string. The question is how to tell serializer not to serialize several model fields and pass them 'as is'.
I found only this dirty hack to do it, i've added afterFind
in my model class:
public function afterFind()
{
$this->rawJson= json_decode($this->rawJson);
}
I'll appreciate any help on this, thanks in advance.
Upvotes: 0
Views: 75
Reputation: 2300
I suggest you leave the original attribute untouched and create a new attribute with a getter-setter pair. Assuming that your original attribute is called rawJson
:
public function getProcessedJson()
{
return json_decode($this->rawJson);
}
public function setProcessedJson($value)
{
$this->rawJson = json_encode($value);
}
Then just add processedJson
to your fields()
method, and you're good to go.
Upvotes: 1