OOP - how do i do this?

This is a basic db access class used with a few databases and many tables.

I want to add in functionality that allows me to collate error information when adding a record.

  1. Person submits form to add as new user.
  2. My script checks to ensure username requested not taken + other tings etc.
  3. details of any errors are then tracked as the checks are made.
  4. If can’t add record, then errors are shown.

Where would I add it and integrate it with this code?


class Database
{
    private $host;
    private $user;
    private $pwd;
    // etc accesses the db and does all the db stuff    
}

class User
{
    private $db;
    private $id;
    public $name;
    
    public function __construct($database, $id)
    {
        $this->db = $database;
        $this->id = $id
    }
    
    public function update_record()
    {
        $this->db->query("UPDATE employee_table SET name='$this->name' WHERE id='$this->id'");
    }
    
    // etc...
}

// Now using these classes is very simple. Every object of type 'User' will 
// need to know what database to work on.

// Instantiate our employee database
$employee_db = new Database();
$employee_db->init('localhost', 'mysql_user', 'mysql_password', 'employee_db_name');

// Instantiate our customer database
$customer_db = new Database();
$customer_db->init('localhost', 'mysql_user', 'mysql_password', 'customer_db_name');

// Now on to the users, lets work with an employee-style user
$some_employee = new User($employee_db);    
$some_employee->name = "John Doe";          // Change some data
$some_employee->update_record();            // Update database record

// And using the second database...
$some_customer = new User($customer_db);
// etc...

If it can’t add the record according to what? According to the checks you’re making or according to the database which finds an integrity constraint or some other kind of error?

If it’s the former, you would handle it in the User class, or in the application logic; it depends on how the rest of the system is set up really.
If it’s the latter IMHO the Database class should inform whatever it was that sent the query to it that the query was unsuccessful.

Can’t add due to error: e.g. requested username already taken etc. (nothing to do with DB which would certainly be in the DB class).

The problem with adding in the User class is that I will have many classes:
User, Product etc…

Wouldn’t doing it in the User (and Product etc.) class be duplicating it (can we use another class somehow just for error logging when attempting to add/update a record)?

As for how things are set up it’s sort of MVC/OOP but there isn’t really any other class to do this error logging in (yet).

Any ideas?

Have you thought about using a Mapper?


$mapper = new DatabaseUserMapper(new PDO('dsn', 'username', 'password'));

#find user
$user = $mapper->read(array('id' => 45));

#change user info
$user->email = 'anthony.sterling@example.org';

#update the record
$mapper->update($user);


class User
{
  protected
    $id,
    $fname,
    $lname,
    $email;
  
  public function __construct($id, $fname, $lname, $email){
    
  }
}

class DatabaseUserMapper
{
  protected
    $database;
  
  public function __construct(PDO $database){
    $this->database = $database;
  }
  
  public function create(array $attributes){
    
  }
  
  public function read(array $criteria){
    
  }
  
  public function update(User $user){
    
  }
  
  public function delete(User $user){
    
  }
}