Can I check if value was changed in prePersist()? - doctrine-orm

I want to automatically set one of the entity fields if it was not manually set. Is there a way to check this? The fields has a default value, so I can not simply compare the value. I was wondering if doctrine maintains whether or not value is changed and if I can access that information.
Also, Is prePersist in Doctrine 2 equivalent of preInsert in Doctrine 1? How can I make sure I only run code on create statement?
Thanks

The prePersist event (docs about prePersist) is triggered when you call Doctrine\ORM\EntityManager#persist on an entity.
If you need to check for changes to an entity, I suggest you to check for the onFlush event (docs about onFlush). There you can obtain any changes you have applied to the entity using the Doctrine\ORM\UnitOfWork API. Changes tracking on an entity happens after calling Doctrine\ORM\EntityManager#persist

Related

How to detect relation change in Ember.js after createRecord

I have
Ember.computed("task.inputValues.[]", ..
Inside this method at certain condition, i trigger creation of new inputValues that have a task set with belongsTo. But after records and relations are successfully set in Ember database (using "createRecord"), "task.inputValues.[]" does not catch/trigger computed method to recalculate?
As Acorncom mentioned "folks recommend avoiding computed properties with side effects (such as creating new records)"
Seems there is some kind of protection in Ember that prohibits recursive triggering of computed property by itself.
The solution was to move records creating code from computed property to Observer, after new records got inserted by the observer, computed property selector "task.inputValues.[]" did trigger.

How to update the properties of CMFCPropertyGridCtrl?

I'm developing an application that binds a data model and a user interface together through MFC and I'm trying to use the CMFCPropertyGridCtrl to display and edit the data that's extracted from the data model.Then after I finish editing the properities shown on the CMFCPropertyGridCtrl, I need to move the new updated data back to the data model. When doing this, I need to check if the data in the CMFCPropertyGridCtrl is really updated before the data transfer is executed. I achieve it through checking the return value of IsModified method. But after I move data back to data model, the CMFCPropertyGridCtrl doesn't update its properties itself. So the IsModified method will never work since it just compares the current value to the initial value, not the updated value. How can I solve this problem?
CMFCPropertyGridCtrl::OnPropertyChanged is designed to track a change in a property and to reflect the Change to your system. This virtual function is called by CMFCPropertyGridProperty::OnUpdateValue.
Because m_bModified is discussed here to here some words about it, because it causes sometimes confusion:
m_bModified is cleared by the function CMFCPropertyGridProperty::ResetOriginalValue! In this case m_varValueOrig is set back to the property. The original value may changed by SetOriginalValue.
So the only good position to check and track changes is CMFCPropertyGridCtrl::OnPropertyChanged. If a property is changed, IsModified is true. But this only compared to the original value...
If you're updating value and want to see your modifications in bold text, then it makes sense to use CMFCPropertyGridProperty::SetValue and CMFCPropertyGridProperty::SetOriginalValue in initialization phase.
But next time you want to get your value to be updated use CMFCPropertyGridProperty::SetValue and then call manually to CMFCPropertyGridCtrl::OnPropertyChanged( pointer to your property )
That function will call protected SetModifiedFlag() function, which in a turn will update protected m_bIsModified to have correct value.

set Doctrine2 entity property without retrieving entire associated object

I have a table Object that has 2 fields that are foreign keys (user_id and teacher_id). After generating the Entities for the X table, the entity only contain the $user and $teacher properties, which forces me to use the associated objects instead of id. So supposing I know the user_id and teacher_id for my object, instead of doing:
$object->setUserId(1)
I have to do:
$user = $this->getDoctrine()->getRepository('MyBundle:Users')->find(2);
$object->setUser($user)
is there no way to work directly with the ids to avoid retrieving the entire object associated to each id?
The framework suggests to use objects when setting the association value. Still – are you sure the record isn't loaded in the memory already? If it is, it will not cause additional SQL statement execution.
If you really need to update the association without loading the object, you can
run native SQL;
try creating Doctrine Proxy object manually and setting it instead.
You can get the proxy object using EntityManager method getReference:
$object->setUser($this->getDoctrine()->getReference('MyBundle:Users', 2));

Django - Insert Without Returning the Id of the Saved Object

Each time the save() method is called on a Django object, Django executes two queries one INSERT and one SELECT. In my case this is usefull except for some specific places where each query is expensive. Any ideas on how to sometimes state that no object needs to be returned - no SELECT needed.
Also I'm using django-mssql to connect to, this problem doesn't seem to exist on MySQL.
EDIT : A better explanation
h = Human()
h.name='John Foo'
print h.id # Returns None, No insert has been done therefore no id is available
h.save()
print h.id # Returns the ID, an insert has taken place and also a select statement to return the id
Sometimes I don't the need the retruning ID, just insert
40ins's answer was right, but probably it might have higher costs...
When django execustes a save(), it needed to be sure if the object is a new one or an existing one. So it hits the database to check if related objext exists. If yes, it executes an UPDATE, orherwise it executes an ISERT
Check documentatin from here...
You can use force_insert or force_update ,but that might be cause serious data integrity problems, like creating a duplicate entry instead of updating the existing one...
So, if you wish to use force , you must be sure whether it will be an INSERT or an UPDATE...
Try to use save() method with force_insert or force_update attributes. With this attributes django knows about record existence and don't make additional query.
The additional select is the django-mssql backend getting the identity value from the table to determine the ID that was just inserted. If this select is slow, then something is wrong with your SQL server/configuration because it is only doing SELECT CAST(IDENT_CURRENT(*table_name*) as bigint) call.

Is there a table which holds all possible states of a determined work Item type in TFS?

I'm developing a Time Tracking system in TFS so we can control how much time is spent in each task. I'm doing it by checking changes in work items states, and recording the time between states.
I'm using WCF and TFS2010 alert subscription.
Then I noticed the State column in the WorkItem table holds a string, instead of an ID pointing to a State.
With that in mind, I noticed I would have to parse each state and check if it corresponds to some string. And then, some day, someone might want to change the State name. Then we're doomed.
But before I hardcore (or put in some random config.xml)... let me ask, is there a table which holds all possible states of a determined work Item type in TFS?
The states of work item types are stored in the process template files. You can export the work item type to an xml file using witadmin.exe and see the allowed values of the "State" in there.
Programmatically, you can use the Microsoft.TeamFoundation.WorkItemTracking.Client namespace to get the WorkItemType object of your work item type, look for the FieldDefinition object of the "State" in the FieldDefinitions property, then get the possible states from the AllowedValues property of FieldDefinition class.