Welcome Guest, Not a member yet? Register   Sign In
Converting DataMapper to JSON
#1

[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 {
                  var $table = 'Tracking_categories';    
    function __construct()    {        parent::__construct();    }
    function post_model_init($from_cache = FALSE) {    }    
}

CONTROLLER
Code:
...
   $category_list = new Tracking_category();  
   $data = $category_list->get();  
   echo json_encode( (array)$data);
   ...

NOTES:
a) I'm using
Code:
json_encode( (array)$data));
instead of 'array' / 'json' extensions, not sure it that makes any difference.
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":"

","error_suffix":"<\/p>","created_field":"created","updated_field":"updated","auto_transaction":false,"auto_populate_has_many":false,"auto_populate_has_one":false,"all_array_uses_ids":false,"valid":false,"local_time":false,"unix_timestamp":false,"timestamp_format":"Y-m-d H:i:s O","fields":["id","master_id","user_id","parent_type","type","parent_category","category","business_related","description","tax_related","memo","active"],"lang_file_format":"model_${model}","field_label_lang_format":"${model}_${field}","all":[{"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":"

","error_suffix":"<\/p>","created_field":"created","updated_field":"updated","auto_transaction":false,"auto_populate_has_many":false,"auto_populate_has_one":false,"all_array_uses



...
[],"trans_enabled":true,"trans_strict":true,"_trans_depth":0,"_trans_status":true,"cache_on":false,"cachedir":"","cache_autodel":false,"CACHE":null,"_protect_identifiers":true,"_reserved_identifiers":["*"],"stmt_id":null,"curs_id":null,"limit_used":null,"stricton":false,"_has_shutdown_hook":true}}
#2

[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.
#3

[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.




Theme © iAndrew 2016 - Forum software by © MyBB