How to implement the Data Mapper design pattern in PHP for data persistence?

Member

by rollin , in category: PHP General , 2 years ago

How to implement the Data Mapper design pattern in PHP for data persistence?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raven_corwin , 2 years ago

@rollin 

The Data Mapper design pattern is a popular pattern for implementing data persistence in object-oriented programming. In PHP, you can implement the Data Mapper pattern by following these steps:

  1. Create a data mapper class that will handle the persistence of your domain objects. This class should have methods to insert, update, delete, and select objects from your database.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class UserMapper {
    private $db;

    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function insert(User $user) {
        // Implementation code for inserting user object into database
    }

    public function update(User $user) {
        // Implementation code for updating user object in database
    }

    public function delete(User $user) {
        // Implementation code for deleting user object from database
    }

    public function select($id) {
        // Implementation code for selecting user object from database based on id
    }
}


  1. Create a domain object class that will represent the data you want to persist. This class should have properties that map to the columns in your database table.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class User {
    private $id;
    private $name;
    private $email;

    public function __construct($id, $name, $email) {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
    }

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getEmail() {
        return $this->email;
    }
}


  1. Use the data mapper class to persist your domain objects. When you create a new domain object, use the data mapper class to insert it into the database. When you want to update an existing domain object, use the data mapper class to update it in the database. When you want to delete a domain object, use the data mapper class to delete it from the database.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$db = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
$mapper = new UserMapper($db);

// Create a new user
$user = new User(null, 'John Doe', '[email protected]');
$mapper->insert($user);

// Update an existing user
$user = $mapper->select(1);
$user->setName('Jane Doe');
$mapper->update($user);

// Delete a user
$user = $mapper->select(2);
$mapper->delete($user);


By using the Data Mapper design pattern, you can keep your domain objects decoupled from your database, making it easier to change your database schema or switch to a different database system in the future.