0

I have a database table (mysql) representing the entity Employee in an enterprise like application. Surrogate keys are used throughout the database, so every Employee record has an auto incremented ID column identifying the Employee. I have modelled the Employee as a Business Object (BO) in php like this:

class Employee 
{
    public $id;    // surrogate key of database record
    public $firstName;
    public $user;
    public $posInCompany // associated EmployeePosition

    public function __construct($emplData)
    {
        $this->id = $emplData['id'];
        // ... init other properties
    }       
}

I am wondering if the $id property should be part of that class? Actually it represents the surrogate key from database and is thus storage-related. But I want the BOs to know nothing about the way they are stored/loaded (seperation of logic from persistence). The BOs hold the business logic for me and I want to work with them WITHOUT any coupling to type of storage (database etc.). With my above class layout I could never instantiate an Employee without giving an $id, so I guess this is not the correct way to implement a BO.

I want to work with DAO pattern to store/load Employee. Should I use the $id only within DAO concerns?

Further I am concerned about identifying an Employee object in my application later, which would work well with the surrogate $id. The $user property is also unique for every Employee in the database. So maybe I should use this property instead?

Michbeckable
  • 1,851
  • 1
  • 28
  • 41
  • I dont know If I understand you correctly.. but if the ID only used in your class and other classes shouldnt get access to this id , use private $id instead of public $id... – goldlife Jan 23 '15 at 08:41
  • No, my question is about the $id being part of the business object at all, considering decoupling from storage but also giving a way to identify an Employee in the application. - I have edited for mor clearance I hope. – Michbeckable Jan 23 '15 at 09:05
  • If you dont need the surrogate id anywhere then dont make it a part of the object. Looks like you can get uniqueness from the $user variable so no need to have the $id in the object – SNAG Jan 23 '15 at 09:07
  • No matter how objects are stored there is always going to be a need to be able to uniquely identify them. That'd indicate you should keep the ID in the BO. (On a side-note, public properties are really dangerous. I could put literally anything in them. You're better off making them non-public and implementing getters/setters that can protect against someone setting the object's props to an invalid state) – GordonM Jan 23 '15 at 09:12
  • But I also have the possibility to identify them by property `$user` in application. So maybe I should use the `$id` in DAO part and `$user` in application part? Hmm, but what about BOs that do NOT have a further unique property? They can only be identified by surrogate key, won't they? Yes getters/setters are better for encapsulation. – Michbeckable Jan 23 '15 at 09:16

0 Answers0