• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding Extending Entity Classes

#1
I am developing a property management database for our company to use in house.  The original database was done in PHP/MySQL without using a framework. It works fine, but when a bug is found or a feature needs to be added, it is extremely difficult to work with the existing code base. I'm re-writing the database with CodeIgniter to make it more maintainable.  I am relatively new to CodeIgniter 4 and frameworks in general.

I am struggling with the best way to implement inheritance in CodeIgniter 4.  In the old database there is a Property object that handles all of the business logic and database calls for each Property in the database.  Multi-tenant buildings have multiple Units associated with each Property stored in a separate table in the database.  A Unit object extends the Property object and handles the database calls and business logic for each Unit and also inherits all of the properties and methods from the Property object. 

For example, if I create a new Unit object, all the database columns from both the Property table and the Unit table are used to hydrate the properties for that Unit object.  The street_num and street columns from the Unit table and the city, state, and zip columns from the Property table are all properties of this Unit object.  So when I echo $unit->getAddress(), the full address is output.

From what I understand about CodeIgniter 4, Entity classes would be the best way to handle this within the framework.  However, Entity classes seem to only represent one row in the database.  Can I extend a Property Entity with a Unit Entity?
Reply

#2
After playing around with Entities for a bit, I still can't determine whether they can be effectively extended for my purposes or not.  However, it does appear that an Entity's attributes array can be populated with with data from joined tables simply by instantiating it with all of the relevant data from the start.

For example, the following method in my UnitModel Model will return a Unit Entity object with data from both the Units and the Properties tables.

PHP Code:
public function byUnitID(string $unit_id '') {
   $unit $this
       
->join('properties''units.property_id = properties.property_id')
       ->where('unit_id'$unit_id)
       ->first();     
   
return $unit;


For my purposes, this negates most of the need to extend the Unit Entity in the first place.  Problem solved!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.