I’m a bit confused about the intent, purpose, and execution behind separating model code into Table and Entity.
In my view, if Table code is supposed to be about talking to the database, then most of the business logic would live in Entity code.
But, CakePHP 4 has all the relationships defined under Table code.
It doesn’t make sense to me to have the relationships defined there, and then use and refine those relationships in a totally different place. Especially considering the database itself has no care about the relationships. CakePHP defines and enforces relationships and the database just stores raw data. Relationships have nothing to do with transferring data to and from the database (aside from preparing the right query to transfer associated data)… nor do relationships have anything to do with formatting on write or translating on read, nor with validating (all of these being examples of functionality that, to me at least, seem right for Table code).
As an example, imagine coding an application that’s designed to manage menus and recipes, taking into account recipe type, ingredients needed, ingredients on hand, chefs available, and seasons/occasions. Now, when the head chef wants to develop a menu, she will load the Menu model from Cake and it will need to pull in data from all those other tables, and have the ability to constrain the list of possible recipes based on those other factors, as well as factors unique to the Menu model (say, a limit of 10 entrees and 5 desserts, minimum profit margins and gross receipts based on price per recipe, and actual cost per recipe, and expected service).
It seems to me that all that logic would be housed within the Entity code for the Menu model. Whereas, data integrity would be managed in the Table code (and be fairly thin, relative to the Entity code).
But… as I said above: CakePHP has all those relationships defined in the Table code. It’s extremely awkward to be separating the relationships from the relationship utilization and management. The fine details and conditions of those relationships are all going to be living in Entity code.
Or am I wrong? Should I be putting all that logic into Table code? If so, what exactly is Entity code for then?
Apologies if this is actually discussed in the docs. I was unable to find it.