Converting DataMapper to JSON |
[eluser]Unknown[/eluser]
I’m trying to call a very simple model, but the get() method is bringing a lot of additional information together with the actual data –highlighted as bold- stored in the database: MODEL Code: class Tracking_category extends DataMapper { CONTROLLER Code: ... NOTES: a) I'm using Code: json_encode( (array)$data)); b) In a separate forum I read something like: "there is one __sleep magic function which gets called when serialization takes place, you can filter your things there, or better make a function in helper taking objects as first parameter and second as fields to be serialized, then return json", but I'm unsure on how to do this or if my reported issue is related with this. RESULT Code: {"table":"sm_tracking_categories","error":{"all":[],"string":""},"stored":{"id":1,"master_id":"[email protected]","user_id":"[email protected]","parent_type":"Expense","type":"Discretionary","parent_category":"Allowances","category":"","business_related":"N","description":"","tax_related":"N","memo":"","active":"Y"},"prefix":"sm_","join_prefix":"","model":"tracking_category","db_params":"","error_prefix":"
[eluser]yacman[/eluser]
Try using php function array_filter using a callback method to only keep data you want. You can just additionally loop through the results and unset like unset($val[0]); or unset($val[0]->xyz); segments of the response. You can also extend the class to implement serializeable. When you json_encode, it will use this implementation to generate the output.
[eluser]WanWizard[/eluser]
You have to remember that get() doesn't return resultsets, it returns $this, meaning the DM object. It is pointless to json_encode that, as it contains lots of properties that are not data related, including some references to CI libraries. The array / json extensions suppied with Datamapper only encode the data in the object. |
Welcome Guest, Not a member yet? Register Sign In |