Re: Inserting/Deleting Records From Within EOGenericRecord
Re: Inserting/Deleting Records From Within EOGenericRecord
- Subject: Re: Inserting/Deleting Records From Within EOGenericRecord
- From: Jonathan Rochkind <email@hidden>
- Date: Wed, 10 Mar 2004 12:26:07 -0600
I don't know if this is responsible for your exception or not, but:
You have a relatinoship to from Subscription to Subscriber. This
relationships is based on the foreign key "subscriberID" in
Subscription. You do NOT need to set this key yourself by hand! In
fact, you should not. The subscriberID attribute should generally not
be marked a class property in Subscription. The "subscriber"
relationship in Subscription should be defined in terms of
subscriberID, true. But the subscriberID itself should not be marked
a class property. EOF takes care of it for you. You set a
relationship from Subscription to Subscriber, and EOF will update the
subscriberID column in your db. You do not need to deal with the
subscriberID attribute yourself, and generally should not.
There are some WO and EOF tutorials on the Apple site which should be
helpful to you. There are also some good third-party WO books these
days.
--Jonathan
At 12:54 PM -0500 3/10/04, email@hidden wrote:
Thanks for the response everyone. I believe this might help the rest
of the mailing list.
----- Original Message -----
From: David Griffith <email@hidden>
If you are using custom java classes, you can instantiate an
instance of
subscription like this:
Subscription myNewSubscription = new Subscription();
Wow! I had no idea! this is so much more elegant :) thanks :)
> 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.
Actually this in this.subscriberId() is actually Subscriber.java
(the EOGenericRecord). Subscription is another EOGenericRecord (or
custom class. Here is the relationship:
Subscription.java <<-> Subscriber.java
So when I try to create a new subscription from within Subscriber.java I do:
//(thanks to you)
Subscription subsription = new Subscription();
//I insert into the editing context
this.editingContext().insertObject(subscription);
//then I set the foreign key for subscription
subscription.setSubscriberId(this.subscriberId());
// then I save the relationships between subscriber and subscription
this.addObjectToBothSidesOfRelationshipWithKey(subscription,"toSubscriptions");
//then I save
this.editingContext().saveChanges();
But I am getting an Attempt to insert nil error here :
NSInvalidArgumentException: Exception while evaluating
takeValue:forKeyPath:'child1.grade', on target:: ***
-[NSConcreteMutableArray addObject:]: attempt to insert nil
>
> 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
relatedto.
I believe I tried that above.
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.
I notice I had to set the foreign keys myself because
> 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.
Thanks I was able to remove it after using the
ec.deleteObject(subscription) method.
_______________________________________________
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.