Please note: this project is inactive since early 2006

 
4.5.10.2 Turning rows into real objects

Say you've presented to your user a large list of objects to choose from. Now the user selects one for modification or detailed inspection, probably including the objects in relations. There you need to get the magic on real objects back.

This is easily done with EditingContext's faultForRawRow; continuing the previous example:

>>> raw_john=ec.fetch('Executive', 'firstName=="John"', rawRows=1)[0]
>>> pprint.pprint(raw_john)
{'firstName': 'John',
 'fkStoreId': <Modeling.GlobalID.TemporaryGlobalID instance at 0x8364a0c>,
 'id': 3,
 'lastName': 'Cleese',
 'officeLocation': '4XD7'}
>>> john=ec.faultForRawRow(raw_john, 'Employee')
>>> john
<Executive.Executive instance at 0x8531db4>
>>> john.getFirstName(),john.getLastName(),john.getToStore().getCorporateName()
('John', 'Cleese', 'We sell parrots')

Here we converted a raw dictionary to a real object, which is automatically registered within the framework with all its normal capabilities, just as if you directly fetch()'ed it from the database.

As you can see, faultForRawRow() takes a dictionary and the name of the entity the object belongs to, and returns the real object. Note that the entity's name shouldn't be exactly the right one: we asked for an Employee, we got an object of class/entity Executive, sub-entity of Employee. The only constraint on entityName is that it needs to belong to the same inheritance tree than the object's. This makes life easier when turning into objects raw rows which were fetched against a whole inheritance tree.

For example, you may fetch raw rows for the whole hierarchy beyond entity 'Employee': the rows you'll get will belong to either entities 'Employee', 'SalesClerk' or 'Executive', but this information is not enclosed within the dictionaries themselves4.1. When turning the raws back to objects, just specify the root entity, and the object of the right class will be automatically returned.



Footnotes

... themselves4.1
except that, if the sub-entities have more attributes than their parent, you'll probably be able to guess to which entity a dictionary belongs to: for instance, only 'Executive' objects have an attribute 'officeLocation'.
Comments are welcome: Sebastien Bigaret / Modeling Home Page
Hosted by:SourceForge.net Logo