An object that acts as a Gateway (466) to a single record in a data source. There is one instance per row.
A Row Data Gateway gives you objects that look exactly like the record in your record structure but can be accessed with the regular mechanisms of your programming language. All details of data source access are hidden behind this interface.
A Row Data Gateway gives you objects that look exactly like the record in your record structure but can be accessed with the regular mechanisms of your programming language. All details of data source access are hidden behind this interface.
Row Data Gateway
An object that acts as a Gateway to a single record (row) in a database. There is one instance per row.
!php
// This is the implementation of `BananaGateway`
class Banana
{
private $id;
private $name;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
Row Data Gateway
Usage
!php
$con = new Connection('...');
$banana = new Banana();
$banana->setName('Super Banana');
// Save the banana
$banana->insert($con);
// Update it
$banana->setName('New name for my banana');
$banana->update($con);
// Delete it
$banana->delete($con);
Row Data Gateway
Under the hood
!php
public function insert(Connection $con)
{
// Prepared statement
$stmt = $this->con->prepare('INSERT INTO bananas VALUES (:name)');
$stmt->bindValue(':name', $name);
$stmt->execute();
// Set the id for this banana
//
// It becomes easy to know whether the banana is new or not,
// you just need to check if id is defined.
$this->id = $this->con->lastInsertId();
}
Row data gateway pattern
An object that acts as a Gateway to a single record in a data source. There is one instance per row.
How it works
Each row in the table is represented as one object. That object has exactly the same fields as the database row, and all the methods to update itself in the database. There is a Finder class which instantiates the object.
When to use it
If you require a slightly higher level of abstraction than the simple Table data gateway pattern, then use it. Fowler uses it most often when using the Transaction script pattern.
No comments:
Post a Comment