Sobes.tech
Junior

What is an entity in the context of programming or databases?

sobes.tech AI

Answer from AI

An entity is an abstract concept representing an object from the real world or an information structure that has a unique identity and possesses certain properties (attributes). In the context of databases or ORM frameworks, an entity often corresponds to a record in a database table or a PHP class that represents this record.

Examples:

  • Person (name, age, address)
  • Product (name, price, description)
  • Order (number, date, list of products)

Main characteristics of an entity:

  • Identity: Each entity is unique and can be identified (for example, by a primary key in a database).
  • Properties (attributes): The entity has a set of characteristics that describe its state.
  • Behavior: The entity may have methods that define its actions or interactions with other entities.

In PHP frameworks such as Doctrine or Eloquent, an entity is often represented as a PHP class:

<?php

// Represents the "Product" entity
class Product
{
    private int $id;
    private string $name;
    private float $price;

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

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

    public function setName(string $name): self
    {
        $this->name = $name;
        return $this;
    }

    public function getPrice(): float
    {
        return $this->price;
    }

    public function setPrice(float $price): self
    {
        $this->price = $price;
        return $this;
    }
}