Core Data - Multiple Entries for a Single Entity - objective-c

So in my app I have an Entity called Cards and another called Transactions.
The Transaction entity have the attributes: Date, Location and Amount. So if a user spends his money in 10 locations, I need to have 10 entries in the Transactions for a single card.
I just started working with Core Data and it's getting messy.
I also use MagicalRecord to work with Core Data.
I was able to CRUD the cards entity, adding, updating etc... is all good.
The thing is, I need to add the transactions to a card and don't know how to start with this relationship. How to add transactions to the card and then fetch the card with all the transactions?
Any insight would be much appreciated.

If I understood you right, then you establish a relation between them.
Card -> Transaction is a To-Many relation. See the right hand pane in xcode for this option.
Add an inverse. Always add an inverse. So you have a not To-Many relation
Transaction -> Card
Right? There is only one card for each transaction?
The rest of the answer depends on how you access the data. I suggest to create a model class for each entity. You know how? Click on the entity then go to file/new/file, select core data then NSManagedObjectSubclass and it will be created for you. This class has methods for adding related items.
myTransaction.card = myCard;
respectively
[myCard addTransactionsObject:myTransaction];
Assuming of course that myCard and myTransaction are the classes and that your to-one relation is named card and the to-many relation is named transactions.

Just set a one-to-many relationship from Card to Transaction Entity, and then simply get all of your (previously added by [card addTransactionsObject:newTransaction] call) transactions using that relation: card.transactions.
Don't forget to add an inverse relation fromTransaction to Card!

Related

How Can I Avoid Sql Circular Reference In This Instance?

I have a simple app and in this app there are 2 roles. Role 1 is a Buyer and Role 2 is a Seller. A seller can setup a post to list an item for sale. A buyer can bid on the item in a post the seller posted.
I've been thinking of a way to prevent circular reference here but I think I am over thinking it or missing another path. What would be the best way to configure this setup?
There is actually no circular reference to worry about here. BuyerBids and SellerPosts are fact tables (where the second has details about the first), and MarketUsers is a dimension table.
The many-to-one relations are toward the dimension table, as is expected.
There is no cycle where you would follow relations from the many side to the one side via many-to-one. For example if you start in SellerPosts you have a many-to-one to BuyerBids and from there to MarketUsers, but from there we don't have an outgoing many-to-one relation linking back to where we started.
In other words, if the graph formed by many-to-one relations (as directed edges) is a directed acyclic graph (which it is, in this case), the model is not circular.

Possibly wrong transaction status in Shopware 6 mails?

All e-mail templates in Shopware 6 reference the transactions payment name and current state by using the first collection entry order.transactions.first.paymentMethod.translated.name and
order.transactions.first.stateMachineState.translated.name.
Is the transaction sorting for e-mails different that the default sorting by createdAt?
Because according to all code examples I have found so far (e.g. AccountOrderPageLoader) the transactions are normally sorted by createdAt.
Based on the sortings I would expect e-mails to show a wrong payment method if the customer switches the payment method after ordering.
So my question is:
Shouldn't all e-mail templates reference the last entry of the transactions collection to really show the latest state? Or is the sorting for e-mails changed somewhere else?
Thanks for clarification.
I think this is a valid concern but given the standard procedures shouldn't become an issue. This is because when using the StateMachineRegistry to transition from one state to another, the existing state entity is being updated instead of a new one being inserted. So if the transition model is being used as it is intended, there should only be one state per combination of order and payment/delivery method that gets updated, with the former states being persisted as state_machine_history entities to retrace state changes.
Technically however, given that the associations are instances of OneToManyAssociationField, it is obviously possible to persist multiple entities of order_transaction or order_delivery per order when using the corresponding repositories. I think the reason for this relation model being used was to, in the future, potentially allow multiple delivery/payment methods when placing a single order. However this currently isn't implemented which is why it is important to use the proper services to transition between states as explained.

Right way to model relationship in Entity Framework Core

I'm working on an ASP.Net Core MVC 2.0 application using Entity Framework Core 2.0. I need to upload a file related to an entity that is getting created by this application.
For example, imagine having to upload an invoice PDF as part of creating a payment entity. This should be a one-to-one relationship.
I'm having some trouble deciding how this should be represented in the Entity Framework data (and object) model. I found some guidelines that say that the entity foreign key should exist on the dependent object. In this case, the invoice would be the dependent object, so I should add the payment ID to the invoice object.
But I won't be downloading the invoice object as often as I would the payment object. And I was planning on a flow where the payment view would have a link to an action on the invoice controller to download the invoice using the invoice ID. So having the Invoice ID on the Payment object would make sense.
So I'm not sure of the best way to handle this.
As an aside, I've seen plenty of samples with uploading/downloading files, but not many that shows them how this might look when the files are associated with an entity in the database.
Does anyone have any suggestions?
I don't have any code that I can share at the moment; if anyone thinks code might help, I can throw something together.
Thanks in advance!
Just wanted to point out that Gert Arnold's comment was the correct answer; the following has examples of how to model one-to-one relationships.
https://learn.microsoft.com/en-us/ef/core/modeling/relationships
Thanks Gert!

Objective-C - Get Selected Object from NSArrayController

I'm trying to create a many-to-many relationship in Core Data, and am having a good amount of trouble doing so - although I'm fairly new at Objective-C. I have two entities that I would like to tie together: Playlists and Songs. Each playlist will have a variety of songs, but each song can also be on several playlists, hence the many-to-many. After some looking around, I found that it may be easiest to do this by implementing a third entity which is essentially the "relationship" between the two. The Playlists would have a one-to-many relationship with the third entity, as would the Songs.. The reason for this would be so I could create / remove that third "relationship" entity when I want to add / remove a song from a playlist, and is better overall for organizing, at least in my opinion.
Last thing to note is that I have three basic tables in my window: A table that lists every playlist, a table that lists every song, and then a table that lists all the songs under whatever playlist is selected in the first table.
I'm currently stuck on how to create the "relationship" third entity based on the objects that are selected in both the playlists and songs arrays/tables. I understand how to create the entity, but don't know how I can call the array controller to give me the selected object. Any help or guidance would be greatly appreciated! Thanks in advance,
Pat

How can I set up a newly created relationship in Core Data on iOS?

So I'm using Core Data in an existing iPhone app, and I've set up two entities: Person and Item. The root view of my app's navigation (currently) shows a list of people, and you drill down from there to items. In short, the hierarchy looks like this:
Person -> Item
I want to add a new entity above Person in the hierarchy, called List:
List -> Person -> Item
Additionally, I want the user's first List to be created for them on startup, and for any People the user's already added to be assigned to that list.
I'm familiar with Core Data's lightweight migration & versioning feature, so I think I know how to add the new entity and relationship, but I'm not sure how to:
Create a List record on app start if they've never had the Lists feature before
Set all existing People records to belong to that new list.
One quick and dirty way would be to add some code to my app delegate's applicationDidFinishLaunching method that performs the migration by (1) checking to see if there are any Lists, (2) if not, creating the default one, (3) fetching all existing People from my data store, (4) setting each Person's list attribute to the newly created default list, and finally (5) saving those changes.
My question is: is there any faster or easier way to do all of that?
That's pretty much what you'd want to do. Use an NSFetchRequest to see if any Listss exist. If not, create one. Then do another request to get all the Persons. Here, instead of assigning the list property of each Person, I'd create an NSSet containing all your Persons and assign that to the List's people property. You did create an inverse property, right?
This is actually a pretty lightweight operation, all tolled, so I wouldn't worry too much about performance. Unless you've got hundreds or thousands of Person objects, your user will probably won't even notice.