0

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");
    }            
}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
XcodeMania
  • 305
  • 5
  • 20

1 Answers1

1

I'm not pretty sure if I understand your question but you can follow these hints.

Model your entities in a correct manner

This means that you need (I suppose) a to-many relationships between AllFamille (AF for brevity) and AllProduit (AP).

For example, create a to-many relationship called toAllProduits from AF to AP. Create a relationship called toAllFamille (an inverse) from AP to AF.

See my prev answer: Setting up a parent-child relationship in Core Data.

Retrieve the correct family

After you created a bunch of families, you then create products and you associate them with a specific family. How?

For example, create a NSFetchRequest to retrieve the specific AF. You will need a NSPredicate. After that, just use that retrieved family and assign it to a product. e.g.

AllProduit * _allProduit = (AllProduit*) [NSEntityDescription insertNewObjectForEntityForName:@"AllProduit" inManagedObjectContext:context];
_allProduit.libelleProduit = nomStr;
// your other properties here...
_allProduit.toAllFamille = retrievedAllFamille; // where this object has been retrieved with the correct request

Hope that helps.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190