The user must create a family in AllFamille
. In a second view the user must create a product in AllProduit
. While creating, the user must choose the family created before.
AllFamille
and AllProduit
are two different entities. How to create a relationship between them?
Family creation:
-(IBAction)save:(id)sender
{
app=[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context = [app managedObjectContext];
AllFamille *famille = [NSEntityDescription insertNewObjectForEntityForName:@"AllFamille"inManagedObjectContext:context];
famille.name = nameFamField.text;
NSError *error;
if (![context save:&error]) {
NSLog(@"Erroor");
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"famCreated" object:self];
}
Product creation:
-(void)addProd:(NSString *)idProd
{
NSManagedObjectContext *context = [app managedObjectContext];
NSError *error = nil;
AllCodeVente * _allCodeVente = (AllCodeVente*) [NSEntityDescription insertNewObjectForEntityForName:@"AllCodeVente" inManagedObjectContext:context];
for (NSManagedObject *obj in app.cdeVenteArray)
{
_allCodeVente.codeVente = [obj valueForKey:@"cdv"];
_allCodeVente.uniteVente = [obj valueForKey:@"uv"];
}
AllProduit * _allProduit = (AllProduit*) [NSEntityDescription insertNewObjectForEntityForName:@"AllProduit" inManagedObjectContext:context];
_allProduit.libelleProduit = nomStr;
_allProduit.familleProduit=familleStr;
_allProduit.stockProduit =qteStockStr;
_allProduit.prixVenteProduit=prixVente;
_allProduit.prixAchatProduit = prixAchat;
_allProduit.idProduit= idProd;
[_allCodeVente addProduitObject:_allProduit];
if (![context save:&error]) {
NSLog(@"Erroor");
}
}