ORM (object-relation mapping)

Object-Relational Mapping (ORM) is a technique that allows a software application to work with a database by representing the data in the database as objects in the application. An ORM is a layer between the application and the database that maps the data in the database to objects in the application and vice versa.

ORMs are commonly used in applications that use a relational database management system (RDBMS) to store data. They allow developers to work with the data in an object-oriented way, rather than having to write SQL queries to retrieve and manipulate the data. This can make it easier and faster to develop applications, as well as reducing the risk of SQL injection attacks.

Entities

An entity is a class that represents a table in a database (or collection when using MongoDB). Each entity corresponds to a specific type of data that is stored in the database. For example, in a blog application, there might be separate entities for users, posts, and comments.

Each entity typically has a number of properties that correspond to the columns in the table. For example, a "user" entity might have properties for the user's name, email address, and password.

You can create an entity by defining a new class and marking it with #[Entity]:

src/Users/UserEntity.php
namespace Assegai\App\Users\Entities;

use Assegai\Orm\Attributes\Columns\Column;
use Assegai\Orm\Attributes\Columns\PrimaryGeneratedColumn;
use Assegai\Orm\Attributes\Entity;
use Assegai\Orm\Queries\Sql\ColumnType;

#[Entity(table: = 'users')]
class UserEntity
{
  #[PrimaryGeneratedColumn]
  public int $id = 0;

  #[Column(type: ColumnType::VARCHAR)]
  public string $firstName = '';

  #[Column(type: ColumnType::VARCHAR)]
  public string $lastName = '';

  #[Column(type: ColumnType::BOOLEAN)]
  public bool $isActive = false;
}

This will create the following database table:

+-------------+--------------+----------------------------+
|                          users                          |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| firstName   | varchar(255) |                            |
| lastName    | varchar(255) |                            |
| isActive    | boolean      |                            |
+-------------+--------------+----------------------------+