1

I'm struggling with this kind of issue and I can't find direct answer to my question through Google.

Let's say we have a table 'users' in the database and it has the following columns: id, username, password, real_name.

Then my problems starts after that.

I know we can make an independent class for that like:

class User
{
    private $_id;
    private $_username;
    private $_password;
    private $_real_name;

    // getters

    // setters
}

Now, should I add functions like fetchById on that class? I mean, is it a good practice?

public function fetchById($id)
{
    // validate param
    // query database
    // copy results to appropriate properties
}

Or should it be done by another class, like UserManager? Then for every result, we convert the array result to of that object?

Also, where should I put functions like fetchUsers where it will fetch multiple users from the database? Functions which deals with multiple records of the same entity.

I am looking for code efficiency and performance.

I know 'some' ideas but I can't justify it. I need help.

I'm currently working with CodeIgniter but I think this problem is PHP OOP in general.

Hashem Qolami
  • 88,138
  • 25
  • 132
  • 151
Craftein
  • 762
  • 5
  • 10
  • 2
    Why don't you look at some PHP ORMs like Eloquent or Doctrine to see how they do it – Mark Baker Feb 03 '14 at 12:53
  • Are you more interested in guidelines for writing your own Database Object abstraction or would you consider adopting an existing library that is specifically dedicated to the situation you are describing? – Anthony Feb 03 '14 at 12:54
  • Guidelines please. I want to understand this stuff first before I start using someone else's library/codes/work. – Craftein Feb 03 '14 at 13:01
  • [This answer](http://stackoverflow.com/a/9685039/1634069) may be of interest to you. The question is specifically about implementing an ACL but [tereško's](http://stackoverflow.com/users/727208/teresko) answer is worth reading. – Benny Hill Feb 03 '14 at 14:39

2 Answers2

1

For me personally, I have my models (objects that represent database tables) extend an abstractModel object that has the ID attirbute and shared static functions like fetchById(). This abstract model also has methods like save() which use the ID of the object to save.

You don't have to have an 'id' field in the table, the id of the model just has to be one of the unique key fields in the table.

Instead of fetchUsers() I have a generic loadAll() static function in the abstract class. Thus you could call Users::loadAll() to get all the models of your users. This means that most of your models can be interfaced with in the same way and reduces duplication of code. Of course if there are methods specific to the model, then you will need to define them in child model.

Programster
  • 11,048
  • 8
  • 43
  • 51
0

Build a class for table management and another for entity.

see other ORMs like doctrine, propel, or frameworks ORM like cakephp.

Mohammad
  • 492
  • 5
  • 4