Welcome Guest, Not a member yet? Register   Sign In
Newbie: Models as data objects, and JSON
#1

[eluser]Lister[/eluser]
Hi guys,

Just dipping my toe into CodeIgnitor. First time I've used a framework for web development and I'm running into a few queries regarding usage.

It is my understanding that Models should be used to represent data objects, so I have a "user" model which represents a user, and has members for name etc, and also functions for DB saving/loading.

My problem comes when I want to send this object to the client via JSON. Because my user object derives from Model, there is ten tonnes of crap in there that I don't want encoding.
eg, the JSON ends up looking like this:

Code:
[{"ID":"1","name":"fred","email":"","type":"0","_parent_name":"User","_ci_scaffolding":false,"_ci_scaff_table":false,"config":{"config":{"base_url":"http:\/\/blah.com\/","index_page":"index.php","uri_protocol":"AUTO","url_suffix":"","language":"english","charset":"UTF-8","enable_hooks":false,"subclass_prefix":"MY_","permitted_uri_chars":"a-z 0-9~%.:_\\-","enable_query_strings":false,"controller_trigger":"c","function_trigger":"m","directory_trigger":"d","log_threshold":0,"log_path":"","log_date_format"

etc etc

Am I going about this the right way? ie Am I using models correctly or should my data objects be seperate?
If I am using it correctly how do you guys encode to JSON? I could easily write a member function to spit out the required encoding, but it seems a bit silly when json_encode() already exists.

Cheers,
Lister
#2

[eluser]BrianDHall[/eluser]
This is really just an issue with the downside of inheritance and object transmission.

You aren't really doing anything wrong, but you have to realize that stock functions like serialize and json_encode are designed to provide export/import functions of PHP data, intentionally with no regard to whether or not you really want _Everything_. If you put super-secret values in your model that you did not want sent to the client under any circumstances, the functions might not necessarily care.

Your needs for the functions are simply different than what they were designed for, so you'll need to either accept all the extra data or write your own functionality, like a json_encode_minimal() or some-such.

It might feel silly at first, but this is a case of only wanting limited functionality when much time was spent to make functions robust and usable for all sorts of things. Some situations require MORE than they provide, and in your case you require less. That's the way the ball bounces, I'm afraid.
#3

[eluser]Lister[/eluser]
Thanks for the reply, and that's fair enough I guess. I don't mind implementing something if it's required, I was just worried I was perhaps using the framework incorrectly.

As an aside, I now have a similar problem at the other end, since the javascript objects created are not "real" objects in that they have none of the methods I've defined in javascript. I guess I'll have to write functions there too to populate real objects with the JSON data. (I appreciate this is now off topic, just wondered if anyone had any comments).

Thanks again,
Lister




Theme © iAndrew 2016 - Forum software by © MyBB