Hey guys:)
So I wanted to have a discussion around the best practices for optional belongTo relations.
Lately I’ve been working with an ordering system. To register what a user has bought I create multiple OrderItems. OrderItems contain links to, the product, the price and displayed product description at the time of purchase. Some of these OrderItems are digital products, and therefore there is sometimes a need to link some of them to other tables in the database, but most often that is not the case.
Modelling this with an ordinary belongsTo would result in columns with a lot of NULL’s, and the occasional id. The other option is creating a link table between OrderItems and invoices, effectively creating an n-m relationship. This is what I’ve done so far, and programmatically picking the first one in the relation. However this also seems like a bad solution. An option i’ve considered is making the primary key the orderItem foreign key, effectively reducing it to a 1-m relation again.
What is your view on modelling these situations, and how do I handle it best with Cake?