JSON encoding all attribgutes for a Model

Hi everyone,

I am trying to JSON encode all attributes for a model, both from DB and not from DB. I am however experiencing difficulty even after doing what the documentation says should work.

So I using the code much like:




CJSON::encode($product->getAttributes())



And in the documentation is states that the default param to get all attributes is true. Thing is I have two predefined (in the model) variables:

  • public $title_type

  • public $deleted

And these do not seem to be returned with the DB attributes of the model. So what I actually get back is:




{"title_id":"26","isbn_10":"","isbn_13":"","isbn_status":"0","issn":"","version":"0","status":"","availability":"0","shelf":"","publisher_id":"","vatable":"0","id":null,"toc":null,"publication_date":null,"consignment":null,"trade":null,"returnable":null,"licence":null,"status_last_confirmed":null,"on_hand":null,"available":null,"on_order":null,"preferred":null,"reorder":null,"unwanted":null,"description":null,"update_time":null,"create_time":null}



Am I missing something to be able to return these two non-db varialbes from the model as well?

I mean, just in case I even added these to the attributeLabels() function in case that had something to do with their return but still nothing.

Thanks,

Unfortunately I eventually fixed this by making an actual __toJSON() function forcing the public attributes to be in there. Ugly as hell which I don’t like :(.

But thanks anyway,

In the end we solved this by using this piece of code when trying to get all the attributes including the non db connected attributes.


$safeAttributes = $model->getAttributes($model->safeAttributeNames);

By doing so it returns all the safe attributes including the non db attributes. Note that ithis will only be the case if there are validation rules for the attributes or they are set as safe.