Model best practices question |
I'm building a REST API with four endpoints (I must say that I just started with CodeIgniter). One of them is to get data, and it receives two parameters id, and code via GET.
The issue is that the data is fetched from five different tables, all related (say table1, table2, ..., table5). Here I have a doubt about what is the right way to build the solution: Option 1: Create a model where I get the information from a SQL that joins all tables filtering on table1 where: (table1.id = id) AND (table1.code = code) AND (table1.status <> 'REJECTED') and have the controller call this model method. For example, in the controller I would have a method that contains something similar to the following: PHP Code: $dataModel = new DataModel(); PHP Code: $table1Model = new Table1Model(); The same query would be for an endpoint that needs to insert or update data in multiple tables. What would be the best option? Is there a better one that I don't realize? Any opinion/help is welcome! Regards.
I would go with the second one : Option 2: Create multiple models.. Though, your organization about tables seemed complicated to me. Especially this part:
Code: $responseData = [
Hello, thank you very much for taking the time to answer.
Maybe with generic names it is complicated the issue of tables. Actually all tables are related. For example: Code: transaction As I need all the data the SQL query would be something similar to this: Code: SELECT transaction.id AS transaction_id, Actually there are a few more tables involved, but I think you get the picture. In other endpoints I need to update multiple tables, either adding records or updating some existing ones.
Create abstraction class/method, getMultiData(), getByCriteria().. Hide your query in this method.
When you don't like your query, just change it in the method. This will not affect the code |
Welcome Guest, Not a member yet? Register Sign In |