Is it possible to obtain the timestamp that is used for optimistic concurrency control from an Objectify entity (or a lower-level part of the Google Datastore infrastructure) and if so, how?
It should be possible to obtain the version timestamp by specifying a field like this in your entity POJO:
#IgnoreSave long __version__;
The version # is found in the Entity properties with that key. If you're looking for official documentation, check javadocs (and source code) for Entity.VERSION_RESERVED_PROPERTY and Entities.getVersionProperty(Entity)
Why This Works
When you load a low-level Entity, it comes pre-populated with a synthetic property named __version__. Simply by adding a field to your Objectify POJO with that name, Objectify will load it out of the Entity. Use #IgnoreSave so that the value is only loaded, never saved.
Related
I have a Posts collection on CosmosDB and each post needs to have an insertion date. Since cosmosDB already inserts the _ts field on the documents, I thought that might use that value instead of inserting my own timestamp manually. However, when I try to put an object (in Java) into the collection I get the following error: 400 Unrecognized field "_ts" (class scc.models.User), not marked as ignorable.
Is there a way to obtain _ts in the objects (I'm not talking about a query to obtain _ts of a specific object but instead for _ts be one of the variables of the object when I retrieve it from CosmosDB).
You could just add a property
#JsonProperty("_ts") private String timeStamp; on your User model. If you give it a value when updating (marshalling) it seems to be ignored, but for transparency you could add #JsonIgnore on the timeStamp field as well. If you want to do more low level adjustments you could also provide your own version of the cosmosdbObjectMapper bean (see AbstractCosmosConfiguration).
I am using the C# SDK (I know you are Java - but maybe it's analogous.)
After retrieving a document, I can access the system property _ts as a DateTime via the property Resource.TimeStamp (within namespace Microsoft.Azure.Documents.)
I'm building an app that allows a user to create reports for advertisers. The entities are set up so that there is a relation between the Report object and the Advertiser object - so that the advertiser has a getReports() method to get them.
I would like to change the app so that instead of actually deleting entities, that it simply changes a "deleted" property to true. That part is no problem, but I'm unsure how to make it so that the getReports() on the Advertiser entity only returns reports for the advertiser that have a deleted property of false.
Please let me know if you have any suggestions how that should be done in accordance with Symfony best practices.
You should look into Gedmo Doctrine Extensions. http://atlantic18.github.io/DoctrineExtensions/
Specifically for your case:
http://atlantic18.github.io/DoctrineExtensions/doc/softdeleteable.html
TLDR; they allow you to configure behavior of your entities in a way you desire, so for example when you "delete" an entity, Gedmo's listeners would set it's deleted value to a current datetime. Now you'd still have that record in your database but with not null value of deleted column marking it 'soft deleted', so when querying, it wouldn't be returned (because Doctrine knows how to query these stuff and would add a condition i.e.: ... where deleted ...) unless you explicitly say you want to see those soft deleted records.
I followed this question to create timestamps that save on create and update maintained by the database: Doctrine2 - Type timestamp - Default value
Problem is that now I get a "value cannot be null" when saving a record using Doctrine. I see in the generated insert that it's trying to save the timestamps as null. Is there a way to avoid Doctrine setting a column on INSERT/UPDATE.
Why do you want letting DB to do Doctrine job?
Doctrine won't set a column at all, if you don't map it. But would you take a look to Timestampable extension?
Another approach is using Doctrine entity listeners. So you can make your own, on PrePersist and PreUpdate and set the time there.
If you're worrying about setters for changing dates, you can at least use dynamic mapping for that fields (just map datetime properties on-the-fly when you really need to change them). Not sure that you can use reflection though, because your entity can be behind the proxy class.
If you still want keeping business-logic in Database, you can map only fields you really want to change (not datetime fields in your case), then write custom Doctrine hydrator (just extend their AbstractHydrator class) that will populate all the fields you need (include datetime). Also you can configure your new hydrator as default (i.e. in Doctrine configuration section in config.yml), so it will work without any adjustments.
I have 2 entities, User and Profile. Profile has in-symfony relation with User, but there is no in-database relation (no foreign key, no cascade) - only simple int column named user_id and nothing more.
Problem is obvious: when i delete user - associated profiles persists, but their user_id points to non-existing user row.
Since I use in-symfony relations when i fetch profile from database it fetches also related user entity. I expected that if there is no row with specific ID, it would just leave null or at least throw an exception or something.
Problem is that symfony creates empty User entity object with only id set. rest of its fields are null.
I know solution would be to create FK, constraints etc ... but I'm not allowed to touch anything in database schema.
How can I manage this problem ? I could even leave those empty object if only i had simple way to determine if they exist in database inside TWIG - so i would know if i can display {{ profile.user.email }} for example.
Fast and dirty solution, as you ask, is to use this test: http://twig.sensiolabs.org/doc/tests/defined.html
But I strongly recommend to rework your entity relations.
Found solution: its fetch: EAGER set to problematic mapping in doctrine.
By default doctrine uses LAZY fetching what results in using Proxy classes generated by doctrine for related entity. That class is almost same as real entity class. Difference is inside getter methods that before returning value performs fetching entity from database.
At this point, when you call getter on such proxy, doctrine tries to find entity in database using its ID, and since it doesn't find anything it throws exception.
When using EAGER fetching doctrine performs fetching of related entities on the same time when it fetches main entity and if it doesn't find it then sets null on relation field.
I have a problem concerning the usage of a DataTransformer.
Basically, I am developing a translation tool for my application whose goal is to be as generic as possible.
For that, I chose to follow that model : Database modeling for international and multilingual purposes
So, in different entities in my application, I have translatable attributes that simply are references to i18n elements. Then, this i18n ID is referenced in Translation table entries, that handle translation strings.
I succeed handling my translation interface, but I now have a problem with my forms : Indeed, I want some of my entities to be created/updated via forms. The problem is that I don't want the user to set a i18n ID for the translatable fields, of course, but a text, so that it can be handled by my application to either update or create the related translation in database.
I thought then that creating a DataTransformer could be a good idea, so that I can get the related translation string from the i18nID that is in my Entity entry (for that way, no problem). But my problem here is for the opposite way :
How can I deal with creating/updating i18n entries in my reverseTransform() method without knowing the entity values context?
Is there any way to get the previous entity values so that I could get the i18 ID that is stored originally in my entity? I understand that a Data Transformer is theorically totally independent from my forms and my entities, but I'm totally blocked about how to handle this case.
Indeed, when I save my entity with my translated string, I have no way to know the entity context in my reverseTransform() method, that would have permitted me to get the i18nID of the entity and to update it.
I just have the string that typed the user, but I can't do anything with that, because I can't know if it is an update or not since I don't have access to my entities.
Do you have any clue to do that? Is trying to use a DataTransformer to perform this a bad idea?
Thank you !