Re: Inserting/Deleting Records From Within EOGenericRecord
Re: Inserting/Deleting Records From Within EOGenericRecord
- Subject: Re: Inserting/Deleting Records From Within EOGenericRecord
- From: David Griffith <email@hidden>
- Date: Tue, 09 Mar 2004 23:09:46 +0100
> I am trying to insert and delete records from within an EOGenericRecord (or
> custom classes generated from the db schema of the EOModel)
>
> First of all is this a good idea ? If its ok, then here is my problem.
>
> When I try to delete a record related to the EOGenericRecord it SEEMS to have
> been deleted but its not.
>
> To be specific, Subscriber is the EOGenericRecord and it has a one to many
> relationship with Subscription. (Subscriber<-(toSubscriptions)->>Subscription)
>
> When I delete a subscription from subscriber by doing the following nothing
> happens:
> this.removeFromToSubscriptions(subscription);
This removes the relationship from subscription to this subscriber.
>
> this.removeObjectFromBothSidesOfRelationshipWithKey(subscription,"toSubscripti
> ons");
This removes the relationship on both sides - effectively doing:
this.removeFromToSubscriptions(subscription)
Subscription.setSubscriber(null) - I'm guessing it would set it to null.
Those two methods remove the relationship in the object graph between the
two objects.
You then need to delete the subscriber from the editing context and save
changes on the editing context to propagate the changes to the database.
Something like this:
EOEditingContext ec = this.defaultEditingContext();
ec objEC=cartItem.editingContext();
objEC.deleteObject(subscriber);
ec.saveChanges();
As I understand it, this asks the object you are trying to delete which
editing context it is registered with, then deletes it from that editing
context. Then when you save changes, it saves it back to the database, in
this case deleting the record.
Now your object graph and database should match.
You shouldn't need to remove the relationships before deleting I think. By
the way, I'm learning myself so I might not be 100% accurate on the above
but it's how I do it.
> and then I actually delete the subscription record by calling the following
> generic method:
>
> EOEditingContext ec = this.defaultEditingContext();
> EODatabaseDataSource ds = new EODatabaseDataSource(ec,table);
> WODisplayGroup genericDisplayGroup = new WODisplayGroup();
> genericDisplayGroup.setDataSource(ds);
> genericDisplayGroup.setObjectArray(new NSArray(record));
> genericDisplayGroup.selectObject(record);
> genericDisplayGroup.delete();
> this.defaultEditingContext().saveChanges();
>
> The above deletes the record from the database but when I print out
> "toSubscriptions" again there is the critter (and it hasn't been deleted from
> the "object graph"? or the relationship graph of the EOGenericRecord
> Subscriber).
This will delete from the database but not from the object graph I think.
You wouldn't really need a display group to do what you did here, just
delete the object from the editing context as above - saving the editing
context will do this for you. I think you are thinking like I did at first
- the truth is WebObjects does more for you than you think...
>
> No errors have popped up as of yet.
>
> So I insert another subscription from within the same Subscriber.java as
> follows :
>
> EOEditingContext ec = this.editingContext();
> Subscription subscription =
> (Subscription)(EOClassDescription.classDescriptionForEntityName("Subscription"
> )).createInstanceWithEditingContext(null,null);
> subscription.setSubscriberId(this.subscriberId());
> ec.insertObject(subscription);
> ec.saveChanges();
>
> Firstly am I right to use this editing context from the EOGenericRecord ?
If you are using custom java classes, you can instantiate an instance of
subscription like this:
Subscription myNewSubscription = new Subscription();
> subscription.setSubscriberId(this.subscriberId());
This looks like you are using the setter method to set a value that you are
getting from the getter method for the same value.
You you myNewSubscriber.subscriberId();
To get the value associated with subscriberId from that object.
You use myNewSubscriber.setSubscriberId(someID) to set the value.
If you are trying to set it to the primary key of the Subscriber, there is
another way to do it. Ask if that's what you want to do.
>
> As I insert another subscription from within the same subscriber it inserts
> ANOTHER subscription into the same subscriber and moves on until it hits the
> lines that ask it to save the changes :
>
> ec.insertObject(subscription);
> ec.saveChanges();
>
> At this point it gives an error :
>
> NSInvalidArgumentException: Exception while evaluating
> takeValue:forKeyPath:'child1.grade', on target:: *** -[NSConcreteMutableArray
> addObject:]: attempt to insert nil
>
Probably you haven't set the relationship to whatever table it is related
to.
So if you inserted a new subscription called mySubscription, and you knew
the subscriber (you had an object of subscriber) you could say:
mySubscription to
addToBothSIdesOfRelationshipWithKey(subscriber,"subscriber");
Assuming the relationship from subscription to subscriber is called
'subscriber' this will set the relationship to be the subscriber object you
passed it. This will set the primary keys and foreign keys to match for
you.
> Where child1 is another to-many relationship Subscriber.
>
> What I don't understand is why a subscription record shows up in
> toSubscription() when I expressly delete it.
Cause it didn't get removed from the editing context I guess.
>
>
> A. Shiraz
> 44 Executive Boulevard
> Elmsford, NY 10523
> (914) 592- 4203 x268
> _______________________________________________
> webobjects-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/webobjects-dev
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.