GLORP and ActiveRecord Polymorphic associations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

GLORP and ActiveRecord Polymorphic associations

Oleg Richards
Hello!

How can i create polymorphic associations in GLORP? For example i have a ResourceSnapshot object, which has location. Location could be one of three or more different kind of objects (warehouse, train, ship, plant) (each of them should be represented by different table). So i have to store location_type, location_id like rails does. But how can i use such things with GLORP? Or is there another way to do it? So i want to execute "mySnapshot location" and that message should query database for proper object and create a class for it.

Cheers, Oleg
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] GLORP and ActiveRecord Polymorphic associations

Alan Knight-2
You can do that, but it'll take you right out of the pleasant realm of ActiveRecord doing stuff for you fairly automatically. This is a fairly unpleasant thing to do in relational databases, where polymorphism isn't part of the vocabulary, and having a foreign key that goes to one of several tables is actively fighting against the relational model, and will prevent you from using things like foreign key constraints.

So basically you have two choices. If you have a horizontal inheritance relationship between the classes, then some of that will get done automatically. But you'd still need to define a VariableJoin for the join on the mapping. In GlorpTest, GlorpInheritanceDescriptorSystem and GlorpHorizontalInheritanceTest have examples, such as they are.

Or, and this is probably easier for a one to one type of relationship, you can define a ConditionalMapping based on the type field. Something like
   locationMapping := aDescriptor newMapping: ConditionalMapping.
   locationMapping
        forField: (aTable fieldNamed: 'location_type')
        attribute: [:object | object location]
        if: [:loc | loc class = Foo]
         useMapping: (locationMapping newMapping: OneToOneMapping) attributeName: #location join: (Join from: (table fieldNamed: 'location_id') to: (fooTable fieldNamed: 'id')).

and repeating the if:useMapping: clauses as necessary. Yes, this could be a lot more pleasant, especially for fairly simple cases like this.


At 03:58 PM 3/29/2008, Oleg Richards wrote:

Hello!

How can i create polymorphic associations in GLORP? For example i have a
ResourceSnapshot object, which has location. Location could be one of three
or more different kind of objects (warehouse, train, ship, plant) (each of
them should be represented by different table). So i have to store
location_type, location_id like rails does. But how can i use such things
with GLORP? Or is there another way to do it? So i want to execute
"mySnapshot location" and that message should query database for proper
object and create a class for it.

Cheers, Oleg
--
View this message in context: http://www.nabble.com/GLORP-and-ActiveRecord-Polymorphic-associations-tp16373883p16373883.html
Sent from the VisualWorks mailing list archive at Nabble.com.

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

--
Alan Knight [|], Cincom Smalltalk Development

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] GLORP and ActiveRecord Polymorphic associations

Oleg Richards
Thank you very much, Alan.

You've told me, that it' fairly unpleasant thing to do in relational databases, but what can i do in that case? How do you recommend me to restructure my data? So i have to store locations of my resource and i need that location to be an object with its own behavior. And i also need to have an actions for resources on that objects. For example: Ammonium - Railroad - Transferring, Ammonium - Railroad - Unloading, Ammonium - Warehouse - Storing, etc.

Oleg

Alan Knight-2 wrote
You can do that, but it'll take you right out of the pleasant realm of ActiveRecord doing stuff for you fairly automatically. This is a fairly unpleasant thing to do in relational databases, where polymorphism isn't part of the vocabulary, and having a foreign key that goes to one of several tables is actively fighting against the relational model, and will prevent you from using things like foreign key constraints.

So basically you have two choices. If you have a horizontal inheritance relationship between the classes, then some of that will get done automatically. But you'd still need to define a VariableJoin for the join on the mapping. In GlorpTest, GlorpInheritanceDescriptorSystem and GlorpHorizontalInheritanceTest have examples, such as they are.

Or, and this is probably easier for a one to one type of relationship, you can define a ConditionalMapping based on the type field. Something like
   locationMapping := aDescriptor newMapping: ConditionalMapping.
   locationMapping
        forField: (aTable fieldNamed: 'location_type')
        attribute: [:object | object location]
        if: [:loc | loc class = Foo]
        useMapping: (locationMapping newMapping: OneToOneMapping) attributeName: #location join: (Join from: (table fieldNamed: 'location_id') to: (fooTable fieldNamed: 'id')).

and repeating the if:useMapping: clauses as necessary. Yes, this could be a lot more pleasant, especially for fairly simple cases like this.


At 03:58 PM 3/29/2008, Oleg Richards wrote:

>Hello!
>
>How can i create polymorphic associations in GLORP? For example i have a
>ResourceSnapshot object, which has location. Location could be one of three
>or more different kind of objects (warehouse, train, ship, plant) (each of
>them should be represented by different table). So i have to store
>location_type, location_id like rails does. But how can i use such things
>with GLORP? Or is there another way to do it? So i want to execute
>"mySnapshot location" and that message should query database for proper
>object and create a class for it.
>
>Cheers, Oleg
>--
>View this message in context: http://www.nabble.com/GLORP-and-ActiveRecord-Polymorphic-associations-tp16373883p16373883.html
>Sent from the VisualWorks mailing list archive at Nabble.com.
>
>_______________________________________________
>vwnc mailing list
>vwnc@cs.uiuc.edu
>http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

--
Alan Knight [|], Cincom Smalltalk Development
knight@acm.org
aknight@cincom.com
http://www.cincom.com/smalltalk

_______________________________________________
vwnc mailing list
vwnc@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc