Welcome Guest, Not a member yet? Register   Sign In
Entities and relationship
#4

Personally, I like to use Entities to help with this. When there is a foreign key referencing another database table, I use SQL joins to pull in foreign data as part of a single SELECT statement.  Then my model instantiates an Entity object where the foreign data, a "child entity", is an attribute of the "parent entity".  This prevents excess database calls and allows me to use all of the "child entity" methods within the "parent entity".

For example, say you have a table of companies and table of employees and you want to select a particular employee with all associated company data.  This is pretty simple to do with an SQL join.  By default, CodeIgniter would typically dump the employee and company data into a single array (or even a single Entity) and you could access it from there.  I would create two Entity classes. In this case, Company and Employee.  In you model, you would need to create a method that would  instantiate an Employee Entity object with the data from the employee table only.  Then instantiate a Company Entity object with the data from the company table as a property of the Employee Entity.

AFAIK, CodeIgniter Entities don't handle inheritance very easily. This is a kind of workaround.
Reply


Messages In This Thread
Entities and relationship - by Skem - 10-14-2021, 01:42 AM
RE: Entities and relationship - by includebeer - 10-16-2021, 07:09 AM
RE: Entities and relationship - by MGatner - 10-23-2021, 05:04 AM
RE: Entities and relationship - by mlurie - 10-26-2021, 09:09 AM
RE: Entities and relationship - by manager - 10-31-2021, 09:44 PM
RE: Entities and relationship - by foxbille - 12-18-2021, 10:14 AM
RE: Entities and relationship - by stopz - 12-20-2021, 02:33 PM
RE: Entities and relationship - by groovebird - 07-05-2022, 11:53 PM
RE: Entities and relationship - by kilishan - 07-06-2022, 06:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB