Author - Roshni Rai Post Views - 9 views

Table Data Gateway pattern with an example in PHP

The Table Data Gateway pattern is a design pattern used in object-oriented programming to provide a way to interact with a database table. This pattern allows you to organize your code so that a single class handles all interactions with a specific database table. This class provides methods for common operations like adding, retrieving, updating, and deleting records from the table. Think of it as a “gateway” through which all table-related operations pass.

Example: Managing a “users” Table in PHP
Let’s understand the Table Data Gateway pattern with an example. Assume you have a users table in your database with columns id, name, and email. We’ll create a PHP class to manage all the interactions with this table.

Step 1: Database Connection
First, we need a way to connect to the database. We’ll create a simple class to manage the database connection.

class Database {
private static $instance = null;
private $connection;
private function __construct() {
$this->connection = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new Database();
}
return self::$instance;
}
public function getConnection() {
return $this->connection;
}
}

Step 2: Table Data Gateway Class
Next, we create the UserGateway class, where we will perform all users table operations.

class UserGateway {
private $db;
public function __construct() {
$this->db = Database::getInstance()->getConnection();
}
// Create (Add a new user)
public function insert($name, $email) {
$stmt = $this->db->prepare("INSERT INTO users (name, email) VALUES (:name, :email)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
return $stmt->execute();
}
// Read (Get all users)
public function findAll() {
$stmt = $this->db->query("SELECT * FROM users");
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Update (Modify an existing user)
public function update($id, $name, $email) {
$stmt = $this->db->prepare("UPDATE users SET name = :name, email = :email WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
return $stmt->execute();
}
// Delete (Remove a user)
public function delete($id) {
$stmt = $this->db->prepare("DELETE FROM users WHERE id = :id");
$stmt->bindParam(':id', $id);
return $stmt->execute();
}
}

Step 3: Using the Gateway
Now, let’s check how you can use the UserGateway class to interact with the users table.

// Create an instance of UserGateway
$userGateway = new UserGateway();
// Insert new user
$userGateway->insert('webner user', 'webner.user@example.com');
// Find all users
$users = $userGateway->findAll();
print_r($users);
// Update user
$userGateway->update(1, 'webner user', 'webner.user@example.com');
// Delete a user
$userGateway->delete(1);

Explanation in Simple Terms

  1. Database Class: Manages connecting to the database. We use it to get a connection.
  2. UserGateway Class: Contains methods for adding, retrieving, updating, and deleting users in the users table.
  3. Usage: Demonstrates how to perform operations like inserting a new user, finding all users, finding a user by ID, updating a user, and deleting a user.

By using this pattern, you keep your database operations organized in one place (the gateway class), making your code cleaner and easier to manage.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri