lithium icon indicating copy to clipboard operation
lithium copied to clipboard

Unset does not seem to unset object property on newly created object

Open Siraris opened this issue 10 years ago • 2 comments

I have an object I am creating for storage in Mongo using Model::create(array()); I store a temporary property on the object for use in an override method of save, and then right before I call parent::save($entity) I unset the property (unset($entity->property) as its not needed anymore. The property is not unset, and when parent::save is called, the property is saved. I can, at this point, say $entity->property = NULL and it updates the value of the property before save, so I'm not sure why unsetting it would still keep a reference. According to: http://li3.me/docs/lithium/data/entity/Document::__unset() I believe this should work. Can anyone cast any light? Am I doing something wrong?

Siraris avatar Jul 04 '15 18:07 Siraris

We'll investigate, in the meantime try using whitelists when saving (also gives you more safety):

$item->save(null, array('whitelist' => array('_id', 'title'));

mariuswilms avatar Jul 21 '15 17:07 mariuswilms

unset() on a Document will indicate to remove a field mirroring behavior/name of mongo's $unset operator. Field removal/rename is not yet implemented in the Exporter.

Even if those capabilities were there: when calling unset() on fields previously added in the same session you'd expect different behavior, than actually causing the field to be removed in the database

All this does not help solve the issue you have now, but explains where it's coming from. Please use whitelists as detailed above, to prevent temporary Document fields to leak into Mongo or use a locked Mongo Schema.

Technical note: Going forward Document/Entity should be refactored to use an array of sequential "ops" (i.e. `[['add' => 'foo'], ['remove' => 'foo]]) and resolve those when building the query (practically a FSM) or use a simple diffing algorithm. Currently these informations is spread out across a handful of properties (there should be one source of truth).

mariuswilms avatar Jun 21 '16 18:06 mariuswilms