Skip to content
Maicon edited this page Sep 7, 2023 · 1 revision

Data Model in PotatoService

In PotatoService, the core of data representation and interaction with databases is handled through data models. These models serve as an abstraction layer over your database tables, allowing you to work with database records as objects.

Defining a Data Model:

To define a data model, extend the EntityModel class. This class provides a suite of functionalities that integrate seamlessly with the Eloquent ORM. Each property in the class typically represents a column in the associated database table.

Here's an example of a data model:

namespace your\namespace;

class Teste extends EntityModel {
    public $timestamps = false;  // Indicates if the model should manage timestamps

    #[Column(name: "id", primaryKey: true)]
    public $id;

    #[MinLength(4)]
    #[Column(name: "nome")]
    public $nome;
}

In this example, the Teste model has two columns: id and nome. The #[Column] attribute helps in mapping the class properties to the database columns. Additionally, validation attributes like #[MinLength] can be used to enforce specific constraints on the data.

Attributes in Data Models:

PotatoService's data model system provides several attributes to customize and validate model properties:

  • #[Column]: Maps a class property to a database column. Additional parameters can be provided, such as indicating if the column is a primary key.

  • Validation Attributes: These attributes, like #[MinLength], provide an elegant way to enforce data validation rules. They ensure data integrity before any database operations.

Dive Deeper with Eloquent:

PotatoService's data model system is built on top of the Eloquent ORM. To understand advanced features, relationships, and optimizations, you can refer directly to Eloquent's documentation on its GitHub repository.

Conclusion:

Data Models in PotatoService ensure a structured and object-oriented approach to database interactions. By utilizing attributes and extending EntityModel, developers can craft efficient, validated, and readable database-related code.