Updated April 3, 2023
Introduction to PHP repository
The repository, in a general way, is responsible for handling the data source operations. The repository provides us with a centralized way of managing our database-related operations, increases the readability of code, and makes it easy to track the exception if it occurs. It also helps us decouple the code from the domain layer. It can be an interface, component, or class that separates the domain and database operations. In this topic, we are going to learn about the PHP repository.
Syntax
Inside this, we can define our more identical database operations responsible for fetching the data from the database, updating them, and deleting them. How we can define this see below;
interface name_oF_interface
{
// logic goes here. Can define the methods which are needed to haled the db operations
}
In the above syntax, we are defining one interface which will contain different methods. This interface keyword is followed by the name of the interface which we want to give. Let’s see one practice syntax for better understanding of the syntax see below;
interface DemoInteface
{
public function cerate(Object $obj);
public function delete($id);
public function update(Object $obj);
//many more methods we can define here.
// but we need to provide their implementation as well in the class which is using this repo.
}
In the above syntax, we have defined new methods for database operations, and we will provide their implementation in the different classes.
How does the repository work in PHP?
As of now, we know that repositories are used to handle the database operations. So now we can say that it acts as a layer between the data tier and domain objects like any other language. It comes up with so many benefits, as it provides us centralized managing of our data operations. Also, repositories help us to deal with entities as well; we can fetch those entities and load them in domain objects. We can change these entities as well.
Basic operations that are performed by using the repositories are as follows;
1. We can fetch the entities from the database and store them in the memory, or we can convert those entity objects into domain objects for better modifications because it is always recommended not to modify entity objects directly.
2. After fetching from the database, we can update those objects and send them for saving into the database by using repositories only. This whole process can be done by using transactions. If any exception occurred while doing database interaction, we could roll back the whole transaction.
3. We can easily handle the exception because all the exceptions will arise at the persistence layer only.
When we work with the repository pattern, we have many layers, which is handled very properly. For example, we have different layers such as:
- Presentation layer
- Service Layer
- Business layer
- Data Access layer
Now we will take one example where we will define some classes and repositories according to them and try to understand how they actually work. Also, we are going to see the flow of the repository see below;
Suppose we have to objects here one is Buyer, and another one is Order. This order will include the Buyer data and other information such as the address where we want the delivery, order name, and so on. So we will divide this into three layers which are described as below;
1. Domain layer: We will create domain objects of all classes available. In our example, it is the Buyer an Order class. Both these classes will be falls under the domain layer of the repository pattern. We will use these objects to hold the data from the database.
2. Persistence layer: This persistence layer is nothing but the repository we can say. At this level, only we create our repository, which will again interact with the database. But for this, we will create an interface that will contain all the methods which are required to handle the operation between a buyer and the order. This layer will also help us to track the exception, and they will fall under the persistence layer only.
classes :
Buyer class
class Buyer
{
public $id;
public $gender;
public $email;
public $firstName;
public $lastName;
}
Order class
class Order
{
public $id;
public $order_name;
public $order_description;
public $amount;
public $quantity;
public $buyerid;
}
Repository:
inteface OrderRepo {
// we can define our methods here according to requirement.
}
As you can see in the above logic that we are dealing with the different layers. This is how the repository pattern works in PHP. It is quite similar to other languages.
Example of PHP repository
In this example, we are creating one repository for calling different methods. But as of now, we are not using db here. So, we will just print the message after calling the methods of Repository.
Employee class :
class Employee
{
public $id;
public $emp_first_name;
public $emp_last_name;
public $gender;
public $email;
public $city;
}
interface EmployeeRepo {
public function findById($id);
public function create(Employee $emp);
public function delete(Employee $emp);
public function update(Employee $emp);
}
Repository implementations for the employee :
class SQLEmployeeRepo implements EmployeeRepo
{
public function create(Employee $emp)
{
print("saving employee data here !!");
}
public function findById($id)
{
print("finding employee data here !!");
}
public function delete(Employee $emp)
{
print("deleting employee data here !!");
}
public function update(Employee $emp)
{
print("updating employee data here !!");
}
}
Employee Controller class:
class EmployeeControllerDemo
{
public function create(EmployeeRepo $repo)
{
$emp = new Employee;
$user->emp_first_name = $_POST['emp_first_name'];
$user->emp_last_name = $_POST['emp_last_name'];
$user->city = $_POST['city'];
$repo->create($user);
// here we are creating emp object and calling creae method.
}
}
Output :
Conclusion
The repository makes our code centralized, more readable, maintainable, and easy to understand. By using the repository, we can easily track the exception and errors if they occurred at the repository level while interacting with the database. It also helps us to decouple our code with less dependency on each other. It acts as a thin layer between the db and persistence layer; some called it part of persistence only.
Recommended Articles
This is a guide to the PHP repository. Here we discuss the introduction, syntax nad working of a repository in PHP along with a different example and code implementation. You may also have a look at the following articles to learn more –