Login  Register

Re: Voyage: Circular references

Posted by EstebanLM on Aug 12, 2013; 4:13pm
URL: https://forum.world.st/Voyage-Circular-references-tp4691940p4703329.html

Hi Sabine,

Sadly there is no automatic solution for your problem.
I managed to resolve that by hacking the mongo description this way:

Trip class>> isVoyageRoot = true

Day class>> isVoyageRoot = false

VehicleTrip class>> isVoyageRoot = false

Now, the trick is that since the document will be something like:


Trip {
        days {
                Day {
                        VehicleTrip { ...  day}
                        VehicleTrip { ...  day}
                }
        }
}

(that's of course, pseudo coding heavily ;) )

Actually, since all VehicleTrips will be part of Day subdocument, you do not need to store them.
What you really need is to have the back-reference when you reconstruct the document

You can achieve that by declaring transient the #day attribute of VehicleTrip and setting the day back when you read Day from the database:

VehicleTrip class>>mongoDay
        <mongoDescription>

        ^ VOMongoTransientDescription new
                attributeName: 'day';
                yourself


Day class>>mongoVehicleTrips
        <mongoDescription>

        ^VOMongoToManyDescription new
                 attributeName: 'vehicleTrips';
                accessor: (MAPluggableAccessor
                         read: [ :day | day vehicleTrips ]
                        write: [ :day :trips |
                                trips do: [ :each | each day: day ].
                                day vehicleTrips: trips  ]);
                 yourself

I think that will solve your embedded-cyclic problem.

yes... it is a bit hacky, but it works perfectly :)

Cheers,
Esteban


On Aug 12, 2013, at 11:33 AM, Sabine Knöfel <[hidden email]> wrote:

> Hi Esteban,
>
> thank you for the solution with the VOMongoToOneDescription.
> This works perfect for me.
>
> I have to come back to the circular references.
> I am not sure if this is a bug.
>
> I have the following model:
>
> Trip ->> Day <->> VehicleTrip
>
> One Trip has N Days.
> One Day has N VehicleTrips.
> The VehicleTrip points back to its Day.
> The Trip is voyageRoot.
>
> But I dont want Day or VehicleTrip to be voyageRoot, too.
> (In my opinion this does not make sense).
>
> If I try to save the trip, I get the endless loop.
> Attention, the Image freezes then. :-)
>
> How would you solve this? Also with the VOMongoToOneDescription solution?
> You can reproduce it with the fileout below.
>
> Regards
> Sabine
>
> This is the fileout:
>
> Object subclass: #Day
> instanceVariableNames: 'date vehicleTrips'
> classVariableNames: ''
> poolDictionaries: ''
> category: 'RKA24-Demo'!
>
> !Day methodsFor: 'accessing' stamp: 'sabineknoefel 8/12/2013 11:13'!
> date: anObject
>
> date := anObject! !
>
> !Day methodsFor: 'accessing' stamp: 'sabineknoefel 8/12/2013 11:15'!
> vehicleTrips: anObject
>
> vehicleTrips := anObject! !
>
>
> Object subclass: #VehicleTrip
> instanceVariableNames: 'day description'
> classVariableNames: ''
> poolDictionaries: ''
> category: 'RKA24-Demo'!
>
> !VehicleTrip methodsFor: 'accessing' stamp: 'sabineknoefel 8/12/2013 11:13'!
> day: anObject
>
> day := anObject! !
>
> !VehicleTrip methodsFor: 'accessing' stamp: 'sabineknoefel 8/12/2013 11:14'!
> description: anObject
>
> description := anObject! !
>
>
> Object subclass: #Trip
> instanceVariableNames: 'days description'
> classVariableNames: ''
> poolDictionaries: ''
> category: 'RKA24-Demo'!
>
> !Trip methodsFor: 'accessing' stamp: 'sabineknoefel 8/12/2013 11:14'!
> days: anObject
>
> days := anObject! !
>
> !Trip methodsFor: 'accessing' stamp: 'sabineknoefel 8/12/2013 10:12'!
> description: anObject
>
> description := anObject! !
>
> "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
>
> Trip class
> instanceVariableNames: ''!
>
> !Trip class methodsFor: 'as yet unclassified' stamp: 'sabineknoefel
> 8/12/2013 11:15'!
> demo
> |theTrip theDay theVehicleTrip|
> theTrip := Trip new description: 'Trip to Munic'.
> theDay := Day new date: Date today.
> theVehicleTrip := VehicleTrip new description: 'with car to fair'.
> theVehicleTrip day: theDay.
> theTrip days: (OrderedCollection with: theDay).
> theDay vehicleTrips: (OrderedCollection with: theVehicleTrip ).
>
> theTrip halt save ! !
>
> !Trip class methodsFor: 'as yet unclassified' stamp: 'sabineknoefel
> 8/12/2013 09:47'!
> isVoyageRoot
> ^true! !
>
>
>
>
>
> |theTrip theDay theVehicleTrip|
> theTrip := Trip new description: 'Trip to Munic'.
> theDay := Day new date: Date today.
> theVehicleTrip := VehicleTrip new description: 'with car to fair'.
> theVehicleTrip day: theDay.
> theTrip days: (OrderedCollection with: theDay).
> theDay vehicleTrips: (OrderedCollection with: theVehicleTrip ).
>
> theTrip inspect
>
>
>
> --
> View this message in context: http://forum.world.st/Voyage-Circular-references-tp4691940p4703303.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>