Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perfecting my MagicalRecord import

Right, just cleaning up this question in a hope of finding an answer.

I've been following this blog to get my import working.

http://www.cimgf.com/2012/05/29/importing-data-made-easy/

I managed to get the import working perfectly when the JSON was in the form...

[
    {
        Name: "John Smith",
        Id: 123,
        Company:
        {
            Name: "Apple",
            Id: 1
        }
    }
]

i.e. when the JSON contains the actual object.

The import will find the object and update the existing object rather than creating a new one.

However, some of the JSON I have has the format...

[
    {
        Name: "John Smith",
        Id: 123,
        CompanyID: 1
    }
]

and no matter what I do it creates duplicates.

Please can someone tell me what the Magical Record userinfo set up is to get this working.

like image 403
Fogmeister Avatar asked Mar 21 '13 18:03

Fogmeister


1 Answers

Thanks for your question I was able to successfully use the import feature of MagicalRecord, though I have been using for some time. I was able to get a solution to your issue.

I guess MagicalRecord makes it a convention to use personID for Person and companyID for Company. Since it's under our control its just a caveat to know. Please correct me if I'm wrong. Just after removing the underscore from properties of entities it didn't crash anymore.

I have fully tested below mentioned solution, please see the demo project code that I used. So feel free to raise questions.

Company
-------------
companyID (unique) //Removed underscore from the property
companyName
-------------
persons

Person
-------------
personID (unique) //Removed underscore from the property
firstName
lastName
-------------
company

For this format :

{
        Id: 1,
        FirstName: "John",
        LastName: "Smith"
        Company :
        {
            Id: 123
        }
    }
}

Following should be the provided in userInfo for the company relationship in person

mappedKeyName : Company (As used in the key of JSON)

relatedByAttribute : companyID (Unique property name in Company Entity)

For this model :

{
    Id: 1,
    FirstName: "John",
    LastName: "Smith"
    CompanyId: 123
}

mappedKeyName : CompanyId (As used in the key of JSON)

relatedByAttribute : companyID (Unique property name in Company Entity)

like image 144
Anupdas Avatar answered Nov 05 '22 14:11

Anupdas