Closed ZtF closed 12 years ago
If I'm reading this right, it's the same thing I have called as import():
function import($data) { foreach( $data as $k => $v) { $this->setProperty($k, $v); } }
Which I have defined in my AppModel extending Shanty_Mongo_Document.
Not really. This is something like export. Imagine a situation when I wish to export data eg. in JSON. And that's exactly I'm trying to do.
On that subject it would be really nice to define a set of 'external' properties that will show on export() or something so there is a clean way to export data from a object without having to unset() all the fields you don't want to pass to a client.
Well that depends... I'd say it would be cleaner to have only a possibility to include internal props (such as _id or _type) or not. Any other properties IMO should be (and are) defined as a second param in find(), all() etc. methods.
I my branch i have a fix for the exclude (because you can not include and exclude fields at the same time). Currently the exclude operation is trying to include _id and _type, but we dont need to include those fields if we are only excluding a few other fields.
I've found overriding export in my own models the cleanest way to go.
Agreed
While i agree that it may be clean in code to over ride export, you are still increasing the overhead in your application by bringing in an entire record and then scaling it back with export later. If you dont need the data, exclude it form your query in the first place and dont transmit it from the mongo server. By grabbing the entire record, your grabbing extra bits that need to transmit over the wire needlessly. By excluding fields in the query you are saving milliseconds all over your application rather than trimming back an export once the time over the wire has already been wasted.
Ok for example in a set I use all the records have created/modified/wholastmodified(and more), while I need those fields in the server side code, the client never has any need to see them. So while I sometimes have to pull them back, I don't really want that information going to the client.
Its not exactly a huge deal to work around, but it would be nice if there was a 'export like' function that only exported data from a list of 'allowed' fields to subsequently encode to JSON or whatever.
I think this is possibly two issues being discussed here. You'd obviously only select what you required from Mongo to keep what's going over the wire as small as possible. But, in terms of making sure you never export something you want to hide, overriding export seems like the best way to go.
You are supposed to do that in your query to mongo:
$mongoObject->all(
array(
/* QUERY PARAMERTERS */
),
array(
'field_to_exclude' => -1,
'field_to_exclude_2' => -1,
)
);
That way in your client side code, you can exclude the fields you want, and in the server side code you can exclude the fields you want. The only problem is, in this fork of the code, you cant exclude fields because Coen has things setup to always include _id and _type, i have modified the Document.php file to allow a user to exclude fields.
I have just committed a change to my branch that will let you do the query above, and then let you properly export (skipping the requirement check)
This makes export() work and query limiting work for keeping OtW transmission minimal.
Perhaps it would be nice to include something like this to Shanty_Mongo_Document. Haven't tested it much and it's just quick and dirty solution but it's good enough for me though.