Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Siraris opened this issue Jul 4, 2015 · 2 comments
Open

Comments

@Siraris
Copy link

Siraris commented Jul 4, 2015

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?

@mariuswilms
Copy link
Member

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 mariuswilms added this to the 1.1 milestone May 24, 2016
@mariuswilms
Copy link
Member

mariuswilms commented Jun 21, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants